Solving Relative Path Problems / Errors Between Command Line and Web Browser Filesystem Functions (file_put_contents(), file_get_contents(), fopen(), etc) in PHP
Filesystem functions like file_exists(), file_put_contents(), file_get_contents(), fopen() etc obey different relative path directives depending on whether the PHP script is run from the command line or from a web browser. There are two basic workarounds for this:
1) Use conditionals to define different relative paths:
if(isset($_SERVER['HTTP_HOST'])) // if being run from web browser
{ $path = path relative to "parent" script or REQUESTED_URI; }
else { $path = path relative to current script's location }
NOTE: From what I can tell, if run from the command line clauses like “../” will not work.
2) Use absolute paths relative to parent script:
$path = dirname($_SERVER['SCRIPT_FILENAME']); // directory path of script being called
include($path."/path/trying/to/reach");
If you are going to be using your script in the command line, you are going to need to define the absolute paths anyway, so you might as well use them all the time rather than use a conditional and figure out both the relative and absolute path.
If I overlooked something, please let me know!