Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relative path not working in cron PHP script

Tags:

php

cron

If a PHP script is run as a cron script, the includes often fail if relative paths are used. For example, if you have

require_once('foo.php'); 

the file foo.php will be found when run on the command line, but not when run from a cron script.

A typical workaround for this is to first chdir to the working directory, or use absolute paths. I would like to know, however, what is different between cron and shell that causes this behavior. Why does it fail when using relative paths in a cron script?

like image 387
Sjoerd Avatar asked Dec 28 '09 13:12

Sjoerd


1 Answers

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.

like image 66
Withfriendship Hiox Avatar answered Sep 22 '22 09:09

Withfriendship Hiox