Running php scripts that include relative paths with cron

  |   By  |  One Comment

I recently had a problem running a php script which had an include() in it using cron. It worked fine in normal testing, but not as a cron job.

The reason is that when browser testing the working diorectory is the script’s directory. Cron uses the php location as the working directory, so the relative paths won’t be the same.

So…

Either use absolute paths or change the working directory.
<?php
chdir(dirname(__FILE__));
include('../filename.php');
?>

name

ABOUT THE AUTHOR - ANDY MOYLE

Andy Moyle is a church leader and web developer. His biggest project is the Church Admin WordPress plugin and app. He also runs, mainly so he can eat pizza.