PHP目录问题 [英] PHP directory problems
问题描述
我对此有疑问:
我得到了文件xxx.php,其中我正在解析一个文件:$ config ['etc'] = parse_ini_file( 'config / config.txt');
I got file xxx.php in which I'm parsing one file: $config['etc'] = parse_ini_file('config/config.txt');
一切都很好,直到使用将xxx.php文件包含到我的yyy.php文件中为止require_once( lang.inc.php);
Everything is fine, until I'm including xxx.php file to my yyy.php file using require_once("lang.inc.php");
然后我收到一条消息:警告:parse_ini_file(config / config.txt):无法打开流:...中没有此类文件或目录...
Then I get an message: Warning: parse_ini_file(config/config.txt): failed to open stream: No such file or directory in ...
当我更改时;
$ config ['etc'] = parse_ini_file('config / config.txt');
$config['etc'] = parse_ini_file('config/config.txt');
至
$ config ['etc'] = parse_ini_file('../ config / config.txt');
$config['etc'] = parse_ini_file('../config/config.txt');
然后我的yyy.php可以正常工作,但xxx.php不能。
then my yyy.php works fine, but xxx.php not... and I'm in stuck.
请帮助我。
推荐答案
如果可以从其他目录调用(或请求)PHP脚本,请尝试使用绝对文件路径。例如,在 example.com/one.php
下执行的PHP脚本的当前目录将与 example.com/folder/two不同。 php
。
Try using absolute file paths if your PHP scripts can be invoked (or requested) from different directories. For example, the current directory of a PHP script executed under example.com/one.php
will be different from example.com/folder/two.php
.
您更改为
$config['etc'] = parse_ini_file('../config/config.txt');
仍然是相对,只是这次只是从当前目录> config> config.txt,而不是之前在同一目录中查找的位置。
is still relative, except this time it's just "one directory up from the current directory > config > config.txt", rather than before, where you were looking in the same current directory.
一个通用的解决方法是:
A generic fix would be to do the following:
$root = $_SERVER['DOCUMENT_ROOT'];
// ... code ...
$config['etc'] = parse_ini_file($root . '/config/config.txt');
使用 DOCUMENT_ROOT
可能会出现问题字段,但在大多数情况下应该没问题(例如,未在CLI下定义 DOCUMENT_ROOT
)。另外,请检查 DOCUMENT_ROOT
是否带有斜杠,即使标准不包含此斜杠,某些主机也可能包含斜杠。如果是这样,请在文件路径的开头删除斜杠。
There are possible issues with using the DOCUMENT_ROOT
field, but it should be fine for the most part (such as DOCUMENT_ROOT
not being defined under the CLI). Also, check if DOCUMENT_ROOT
has a trailing slash, even though the standard is not to have it, some hosts may have the trailing slash included. If it does, remove your slash at the beginning of your file path.
这篇关于PHP目录问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!