我遇到了需要一些文件的问题,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';
测试/分/ 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';
到
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告诉我文件不存在,完全在文件所在的位置。
答案 0 :(得分:6)
更改
set_include_path(dirname(__FILE__));
require '../test2.php';
到
set_include_path(dirname(__FILE__)."/../");
require 'test2.php';
答案 1 :(得分:1)
这可能是符号链接问题?尝试:
set_include_path(realpath(dirname(__FILE__))); // added realpath here
还可以尝试:
require(dirname(__FILE__) . '/../test2.php');
答案 2 :(得分:0)
在类似情况下,检查目标(PARENT)文件夹是否存在。