I have a PHP script that essentially copies a zip file, unzips it, and then copies the resulting text files to a MySQL database.
This works perfectly when I call my php page manually, but this needs to be done every day, so I want to set this up as a cron job.
I have a cron job that works, and I have verified that by calling a very simple script, but it fails when trying to run my full page.
I have tracked this down to two areas of code. Firstly:
date_default_timezone_set('Europe/London');
I understand that I can set this up through an htaccess or php.ini file, so I am not concerned about this one.
However, the second area of code that is stopping the cron job from running is:
$localzipfile = "myfolder/myzipfile.zip";
$localzipfilefullpath = "/homepages/20/dXXXXXXXXX/htdocs/sub/folder/" . $localzipfile . "";
$localpath = pathinfo(realpath($localzipfile), PATHINFO_DIRNAME);
$zip = new ZipArchive;
$res = $zip->open($localzipfilefullpath);
if ($res === TRUE) {
$zip->extractTo($localpath);
$zip->close();
}
Again, this all works perfectly when I run the code manually, so I know everything is in place and works. All paths are correct, and the zip file unzips as expected.
It is only when I try to run this as a cron job that it fails and doesn't unzip the file.
I have seen several other SO questions about php files that run manually but don't run as cron jobs, but none that relate to the unzipping of a local zip file.
UPDATE:
I have managed to log the error output from the cronjob, and it is reporting this:
"Cannot instantiate non-existent class: ziparchive"
I don't understand this, though, as the code all runs without issue when I run it from the browser?