Could not open stream: no such file or directory, yes there is!

I have a problem with requiring some files, PHP tells me that these files do not exist, but when I look at the directory, it tells me that it exists.

I have simplified files to functions requireand it still does not work.

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 (file that for some reason is not 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 dynamic)
I thought this would work:
*/
set_include_path(dirname(__FILE__));
require '../test2.php';

EDIT

Well, 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 conclusion:

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 losing my mind when I read errors that seem like PHP to me, saying that the file does not exist, exactly where the file is.

+5
2

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

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

? :

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

:

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

All Articles