Ben Spi
Ben Spi

Reputation: 806

php does not create a file when executed with cronjob

I have a very simple php file, that creates a text document everytime it runs:

$date = date('Y-m-d H-i-s', time());
$log_name = "Cronjob". $date;
if($fh = fopen($log_name,'w')){
    fwrite($fh, print_r($date));
    fclose($fh);
}

When I run the file using my browser a txt file is created on my server. When I run the file with a cronjob, it does NOT create a txt file. When I go to private > cron.log on my server I see that the cronjob works fine and the result is printed into the cron.log.

Can someone tell me why? I need to write that file on the server :)

Upvotes: 2

Views: 1905

Answers (1)

DocRattie
DocRattie

Reputation: 1423

When executed in a cronjob, you need to provide the full path. The script does not executed in the directory it's located in, and therefor relative paths won't work.

$absolut_path = "whatever your absolutpath is"; // maybe something like /var/www/html/
$log_name = $absolut_path."Cronjob". $date;

Upvotes: 6

Related Questions