Relative path not working in cron PHP script Relative path not working in cron PHP script php php

Relative path not working in cron PHP script


Change the working directory to the running file path. Just use

chdir(dirname(__FILE__));include_once '../your_file_name.php'; //we can use relative path after changing directory

in the running file. Then you won't need to change all the relative paths to absolute paths in every page.


The working directory of the script may be different when run from a cron. Additionaly, there was some confusion about PHPs require() and include(), which caused confusion about the working directory really being the problem:

include('foo.php') // searches for foo.php in the same directory as the current scriptinclude('./foo.php') // searches for foo.php in the current working directoryinclude('foo/bar.php') // searches for foo/bar.php, relative to the directory of the current scriptinclude('../bar.php') // searches for bar.php, in the parent directory of the current working directory


The only chance I got "require_once" to work with cron and apache at the same time was

require_once(dirname(__FILE__) . '/../setup.php');