2

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 :)

2
  • Could you use full path to $log_name? It was created but it could be somewhere. So, it is better to use full path to see if the file is created. Commented Apr 19, 2016 at 7:31
  • secondly check whether the directory you are using have full control? like read/write to the user? Commented Apr 19, 2016 at 7:48

1 Answer 1

6

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;
Sign up to request clarification or add additional context in comments.

3 Comments

does that mean that every path in my php files need to be with the absolute path? E.g. that function in another php file woun't work: rename("/web/something.html", "/web/something-else.html");
ok, it works perfectly now: I use the following statement with dirname(FILE) to solve the problem: $log_name = dirname(FILE)."/Cronjob". $date;
"/web/something.html" is a full path, so that should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.