skip to Main Content

I have sript.php

When I run the script in a linux terminal it works and the file is saved.
When I set it in crontab, the script starts, but the file is not saved.
I also changed the directory and file chmod permissions to 777.

$url = 'https://www.example.cz/example.xml'; 
$file_name = basename($url);
if (file_put_contents($file_name, file_get_contents($url))) 
{ 
    echo "File downloaded successfully"; 
} 
else
{ 
    echo "File downloading failed."; 
}

Thank you for the advice.

2

Answers


  1. Looks to me as it works from the terminal as you are running the script in the same directory as the output file. The crontab will need the full path on the server, not just the file name. You need to add the full path to file name on this line,

    $file_name = ‘/path/to/file/’ . basename($url);

    This should work assuming the user executing the cron script has the required permissions to the output file and its directory.

    Login or Signup to reply.
  2. Basically, your cron job executes according to these instructions:

    So, you will need to search for the location of example.xml in your system:

    sudo find / -name example.xml
    

    see where the find is. If it’s located at /home/someuser/example.xml, then the issue you have is that you are using a relative path and the user who executes the cron job saves at his space rather than the place you expect. To fix this, use an absolute path, like "/home/youruser/some/folders/example.xml" so there will be no confusions about it.

    Since you have set all permissions to the folder, I will assume that there will be no issues saving the file to the appropriate location if absolute path is specified.

    It’s also possible that you will not find example.xml, which could mean that the user executing the cron job has no privilege to save the file at the relative location specified.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search