所以我不太确定为什么我的代码不起作用,但这是我得到的错误:
Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "../../setting.xml" in /home1/sk8ermid/public_html/MVP/themes/SDS-2013/home.php on line 3
这是我使用的代码:
<?php
$settings = simplexml_load_file('../../setting.xml');
$site_title = $settings->title;
?>
我正在尝试进入“settings.xml”文件所在的两个目录。我这样做的方式是否正确,还是有其他方法可以做到这一点?
最佳答案
试试这个
$settings = simplexml_load_file(__DIR__ . '/../../setting.xml');
始终牢记 .
(CWD)是任何 include
/require
树根目录下的 PHP 脚本目录并且仅当您配置的 include_path
实际包含 .
时(默认情况下会这样做,但可以在代码中更改)。
如果您的 home.php
脚本包含在不同目录中的另一个脚本中,则 .
是相对于该其他脚本的。
使用 __DIR__
确保您始终从当前脚本的父目录开始。
关于php - 无法使用 simplexml_load_file 进入目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19992411/