Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to open stream: No such file or directory, yes there is!

Tags:

php

require

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.

like image 329
Kokos Avatar asked Jun 16 '11 15:06

Kokos


2 Answers

Change

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

to

set_include_path(dirname(__FILE__)."/../");
require 'test2.php';
like image 92
genesis Avatar answered Nov 15 '22 14:11

genesis


It could be a symlinks issue? Try:

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

Also try:

require(dirname(__FILE__) . '/../test2.php');
like image 36
iamjonesy Avatar answered Nov 15 '22 15:11

iamjonesy