【问题标题】:Failed to open stream: No such file or directory, yes there is!无法打开流:没有这样的文件或目录,是的!
【发布时间】: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

标签: php require


【解决方案1】:

改变

set_include_path(dirname(__FILE__));
require '../test2.php';

set_include_path(dirname(__FILE__)."/../");
require 'test2.php';

【讨论】:

  • 如果您阅读了我的问题(您复制粘贴的部分),您会发现我知道这是可行的,但这种方法对我来说是不可能的。
  • 您的代码仍然错误,但感谢您的编辑,我能够找到解决方案。如果您可以将其编辑到您的问题中,我可以接受答案:set_include_path(dirname(__FILE__)."/../");require('test2.php');。出于某种原因,将 ../ 放在 include_path 中是可行的,而将 ../ 放在 require 函数中则不行..
  • 现在有一个针对此常见错误的故障排除清单:stackoverflow.com/a/36577021/2873507
【解决方案2】:

可能是符号链接问题?试试:

set_include_path(realpath(dirname(__FILE__))); // added realpath here

也试试:

require(dirname(__FILE__) . '/../test2.php');

【讨论】:

  • 好的!不是我第一次看的时候
  • 现在有一个针对此常见错误的故障排除清单:stackoverflow.com/a/36577021/2873507
【解决方案3】:

在类似情况下,检查目标(PARENT)文件夹是否存在。

【讨论】:

    猜你喜欢
    • 2017-05-04
    • 1970-01-01
    • 2013-01-13
    • 2015-07-29
    • 2015-11-13
    • 2011-07-04
    相关资源
    最近更新 更多