views:

46

answers:

3

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?

+1  A: 

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.

Archimedix
You also need to have '.' in the include_path for this to work.
symcbean
There is, as can be seen in the error message shown in the question.
Archimedix
+3  A: 

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.

tdammers
A: 

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

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