0
votes

php include_once is failing for me with the "failed to open stream: No such file or directory" message, despite the existence of the file it is complaining about, and chmod'ing to 777 in order to try to mitigate the problem.

warning: include_once() [function.include]: Failed opening '../i_utility/WCHelper.php' for inclusion (include_path='.:/usr/share/pear:/var/www/html/we_/') in /var/www/i_/php/w_corner/modules/i_ers/i_ers.module on line 6.

From the command line I can cd to the directory containing the source code file (/var/www/i_/php/w_corner/modules/i_ers), and I can ls the file about which php complains "no such": ../i_utility/WCHelper.php

Indeed, I can do the above after su'ing to the apache user, and can even touch the file and see its last modified timestamp changed:

-bash-3.2$ whoami
apache

-bash-3.2$ cd /var/www/i_/php/w_corner/modules/i_ers
-bash-3.2$ ls -l ../i_utility/WCHelper.php
-rwxrwxrwx 1 root root 32112 Sep 14 09:49 ../i_utility/WCHelper.php

-bash-3.2$ touch ../i_utility/WCHelper.php
-bash-3.2$ ls -l ../i_utility/WCHelper.php
-rwxrwxrwx 1 root root 32112 Sep 27 17:08 ../i_utility/WCHelper.php

In light of all of the above, what could be causing PHP's include_once to fail under these circumstances?

3
the file's mode (777) doesn't come into play here as permission issues usually result in a permissions error, rather than a "No such file or directory" error.webbiedave
possible duplicate of php include_once not workingBrent Washburne
@BrentWashburne How can my question be a duplicate of one asked after mine?Dexygen
Fair question, but are they duplicates of each other? If so, which one has the better answer? I think the other one does and that's why I voted this way.Brent Washburne
@BrentWashburne Is the other one better because it has 12 up-voters versus 4? The top answer below does better at explaining as far as I'm concerned. Anyway doesn't matter, it would take quite some time for people to keep re-visiting these questions, and I'm not going to lose any repDexygen

3 Answers

4
votes

PHP's include paths are a bit counter-intuitive, especially when you feed it relative paths. You'd expect the path to be relative to the current file (just like with CSS for example), but PHP interprets them as relative to the current working directory, which is usually the directory where the entry point PHP file resides (e.g. /var/www/my_site/index.php).

The easiest fix is to make use of dirname(__FILE__), which always maps to the absolute location of the current script file.

1
votes

Maybe your script is running in the wrong directory ?
Put an echo '<pre>'.htmlentities(getcwd()).'</pre>'; before the failing include and see if it shows the proper path.

0
votes

why not use the full path to the file? eg.

include_once("/var/www/i_/php/w_corner/modules/i_utility/WCHelper.php");