Failed to open stream: No such file or directory,

2019-04-26 14:48发布

I've got a problem with requiring some files, PHP is telling me these files do not exist, but when I scan the directory it tells me it does exist.

I've simplified the files to the require functionality, and it's still not working.

Here is my setup:

root/
    test.php
    test/
        test2.php
        sub/
            test3.php

test.php

echo    'test';
require 'test/sub/test3.php';

test/test2.php (the file that for some reason doesn't get included)

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';

EDIT

Okay, when I changed this:

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

to

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

it works. wtf php?


Now this is my output:

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

If I add the following code to test3.php:

echo '<pre>';
print_r(scandir(dirname(__FILE__).'/../'));
echo '</pre>';

I get (as expected) the following:

Array
(
    [0] => .
    [1] => ..
    [2] => sub
    [3] => test2.php
)

I think I'm going insane, when I read the errors it looks to me like PHP is telling me a file doesn't exist, exactly in the place where the file is.

标签: php require
2条回答
Fickle 薄情
2楼-- · 2019-04-26 15:12

Change

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

to

set_include_path(dirname(__FILE__)."/../");
require 'test2.php';
查看更多
孤傲高冷的网名
3楼-- · 2019-04-26 15:19

It could be a symlinks issue? Try:

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

Also try:

require(dirname(__FILE__) . '/../test2.php');
查看更多
登录 后发表回答