9

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.

Kokos
  • 9,051
  • 5
  • 27
  • 44
  • Just for the sake of debugging, skip all the fancy path generation stuff, and try requiring via `require('/root/test/test2.php')` and see what happens. if that works, then it's something with your logic. – Marc B Jun 16 '11 at 15:09
  • Yes this works, the whole problem is that I don't/can't know the path in that file. If I could write down the required url like that I would have done it ;) – Kokos Jun 16 '11 at 15:13
  • One often runs into this error, and to quickly troubleshoot it, follow these steps : http://stackoverflow.com/a/36577021/2873507 – Vic Seedoubleyew Apr 12 '16 at 17:02
  • Possible duplicate of [Failed to open stream : No such file or directory](http://stackoverflow.com/questions/36577020/failed-to-open-stream-no-such-file-or-directory) – Vic Seedoubleyew Apr 30 '16 at 14:22

3 Answers3

6

Change

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

to

set_include_path(dirname(__FILE__)."/../");
require 'test2.php';
Kokos
  • 9,051
  • 5
  • 27
  • 44
genesis
  • 50,477
  • 20
  • 96
  • 125
  • If you read my question (the part you copy-pasted) you can see I know that works, but this approach is not possible for me. – Kokos Jun 16 '11 at 15:10
  • Your code is still wrong, but thanks to your edit I was able to find a solution. If you could edit this into your question I can accept the answer: `set_include_path(dirname(__FILE__)."/../");` and `require('test2.php');`. For some reason putting the `../` in the include_path works, while putting the `../` in the require function doesn't.. – Kokos Jun 16 '11 at 15:15
  • There is now a troubleshooting checklist for this frequent error here : stackoverflow.com/a/36577021/2873507 – Vic Seedoubleyew Apr 12 '16 at 17:03
3

It could be a symlinks issue? Try:

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

Also try:

require(dirname(__FILE__) . '/../test2.php');
iamjonesy
  • 24,732
  • 40
  • 139
  • 206
0

In similar cases, check if destination (PARENT) folder exists.

T.Todua
  • 53,146
  • 19
  • 236
  • 237