【发布时间】:2011-09-16 11:19:16
【问题描述】:
我在需要一些文件时遇到问题,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';
到
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 告诉我一个文件不存在,完全是在文件所在的位置。 p>
【问题讨论】:
-
只是为了调试,跳过所有花哨的路径生成内容,并尝试通过
require('/root/test/test2.php')请求,看看会发生什么。如果可行,那么这与您的逻辑有关。 -
是的,这行得通,整个问题是我不/不知道该文件中的路径。如果我能像那样写下所需的网址,我会这样做的;)
-
经常会遇到这个错误,要快速解决它,请按照以下步骤操作:stackoverflow.com/a/36577021/2873507