我遇到了一些需要引入文件的问题,PHP告诉我这些文件不存在,但是当我扫描目录时,它告诉我它存在。我已经简化了代码使用require
功能,但仍然无法正常工作。
这是我的设置:
root/
test.php
test/
test2.php
sub/
test3.php
test.php
echo 'test';
require 'test/sub/test3.php';
test/test2.php(某些原因未被包含的文件)
echo 'test2';
test/sub/test3.php
echo 'test3';
/*
because we are still on test.php, the include path is the root
that means the following would work:
require 'test/test2.php';
however I don't know this path in this file. (it's dynamic)
I thought this would work:
*/
set_include_path(dirname(__FILE__));
require '../test2.php';
编辑
好的,当我更改了这个:
set_include_path(dirname(__FILE__));
require '../test2.php';
to
set_include_path(dirname(__FILE__)."/../"));
require 'test2.php';
它运作了。wtf php?
现在这是我的输出:
testtest3
Warning: require(../test2.php) [function.require]: failed to open stream: No such file or directory in siteroot/test/sub/test3.php on line 6
Fatal error: require() [function.require]: Failed opening required '../test2.php' (include_path='siteroot/test/sub') in siteroot/test/sub/test3.php on line 6
如果我向
test3.php
添加以下代码:echo '<pre>';
print_r(scandir(dirname(__FILE__).'/../'));
echo '</pre>';
我得到了如下结果(与预期相符):
Array
(
[0] => .
[1] => ..
[2] => sub
[3] => test2.php
)
我感觉自己快疯了,当我读到错误信息时,PHP告诉我一个文件不存在,而这个文件恰好就在那个位置。
require('/root/test/test2.php')
进行引用并查看结果。如果这样可以工作,那么问题就出在你的逻辑上。 - Marc B