include() fails when running from terminal but not from the web
I have a PHP script that includes another file in an adjacent directory.
Example code:
include("../lib/file.php");
The code works fine when you a开发者_JS百科ccess the page running it from a browser but fails (on the mentioned include() line) if I run it from a terminal or cron.
I get a warning that says "No such file or directory in /path/to/file.php" and as a result the class I try to call from that file doesn't exist, which leads to a fatal error.
What would cause this to work from a browser but fail from terminal.
Always always always use absolute paths with include:
include(dirname(__FILE__) . "/../lib/file.php");
Always use absolute paths.
As a second thought, running php from terminal will execute php as cli, so the php.ini used is different (usually, /etc/php5/cli/php.ini
instead of /etc/php5/apache2/php.ini
if your web service use apache's mod_php or /etc/php5/cgi/php.ini
if it runs php as cgi/fastcgi mode) so the configuration may be different (include paths, in your case may matter).
精彩评论