views:

147

answers:

4

I have a cron job that needs to include this file:

require '../includes/common.php';

however, when it is run via the cron job (and not my local testing), the relative path does not work. the cron job runs the following file (on the live server):

/home/username123/public_html/cron/mycronjob.php

and here's the error:

Fatal error: require(): Failed opening required '../includes/common.php' 
(include_path='.:/usr/lib/php:/usr/local/lib/php') in 
/home/username123/public_html/cron/mycronjob.php on line 2

using the same absolute format as the cron job, common.php would be located at

/home/username123/public_html/includes/common.php

does that mean i have to replace my line 2 with:

require '/home/username123/public_html/includes/common.php';

?

thanks!

+1  A: 

Technically seen the php script is run where cron is located; ex. If cron was in /bin/cron, then this statement would look for common.php in /bin/includes/common.php.

So yeah, you'll probably have to use fullpaths or use set_include_path

set_include_path('/home/username123/public_html/includes/');
require 'common.php';
Robus
to make debugging easier, can i say this: if(file_exists('/home/username123/public_html/includes/common.php')) { set_include_path('/home/username123/public_html/includes/'); require 'common.php'; } else require '../includes/common.php';or do i need to change it manually every time and do this: // LIVE set_include_path('/home/fini7463/public_html/includes/'); require 'common.php'; // DEBUG //require '../includes/common.php';?
gsquare567
Im guessing the first one should be fine
Robus
+1  A: 

nono. you need to use absolute paths on crons.

what I do is:

// supouse your cron is on app/cron and your lib is on app/lib
$base = dirname(dirname(__FILE__)); // now $base contains "app"

include_once($base . '/lib/db.inc');

// move on
Gabriel Sosa
No you don't... but you have to allow for the fact that you might be running in a different directory, and with a different include_path
Mark Baker
yeah you are right, I think a better wording could be *its recomended* instead *you need*. well I always try to do that
Gabriel Sosa
A: 

If the relative path doesn't work, then it means that the current directory set when the cron tasks are running is not /home/username123/public_html. In such cases, you can only use an absolute path.

kiamlaluno
A: 

It sounds as simple as just some script you are running is setting the include_path and you are including that script. use phpinfo() to check the include_path global vs local setting.

MANCHUCK