WJN
WJN

Reputation: 71

PHP Cron Job does not give correct directory path

I have a PHP module that is in my public_html directory (for example mycronjob.php)

If i execute mycronjob.php from the browser it works as it should.

For example the command getcwd() gives /home/myaccount/public_html and all fopen commands open files in public_html.

However, if I execute exactly the same module as a cron job the command getcwd() gives /home/myaccount and all fopen commands open files in myaccount instead of public_html

The Cron Job is : /usr/local/bin/php -e /home/myaccount/public_html/mycronjob.php

Does anyone have any idea why the cron job getcwd() and fopen() commands would not be resolving to public_html ? My Host seems to be struggling to find the cause.

A typical fopen() command is:

$myfile='mydata.htm';
if(($myfile = fopen($myfile,'w')) === FALSE){
echo "Failed to open myfile file for writing!" . "<br />\n";
exit(8);
}

Upvotes: 1

Views: 333

Answers (1)

Antony Thompson
Antony Thompson

Reputation: 1638

It's because of where the cron gets executed from. It's pretty normal and you can fix it by using the FILE constant.

$file = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mydata.htm';

Upvotes: 3

Related Questions