无法打开流:没有这样的文件或目录,是的!

问题描述 投票:8回答:3

我需要一些文件时遇到问题,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告诉我文件不存在,完全在文件所在的位置。

php require
3个回答
7
投票

更改

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

to

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

2
投票

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

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

也可以尝试:

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

0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.