0

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
R2D2
  • 2,620
  • 4
  • 24
  • 46

3 Answers3

1

Are you using relative pathes for $localzipfile and $localpath?

If yes, try to use absolute ones or write a shell script that chdirs into the PHP script's directory before running PHP.

devnull
  • 558
  • 4
  • 18
  • $localzipfile = "daily/newzipfile.zip"; – R2D2 Aug 03 '15 at 12:32
  • $localpath = pathinfo(realpath($localzipfile), PATHINFO_DIRNAME); – R2D2 Aug 03 '15 at 12:32
  • So, $localzipfile is relative. I assume, since the cron does not have the PHPs script dir as working dir, the script can not find $localzipfile. What happens, if you set $localzipfile the zip file's full path? Does it then work ? – devnull Aug 03 '15 at 12:44
  • I have updated my code and question with full path to $localzipfile, but code still isn't unzipping the file when it runs. – R2D2 Aug 03 '15 at 14:10
1

The paths to your file need to be absolute, for example:

$localzipfile = "/home/myfolder/myzipfile.zip";
$localzipfilefullpath = "/var/www/html/homepages/20/dXXXXXXXXX/htdocs/sub/folder/" . $localzipfile;

Are you sure that the user running cron has permissions to access both directories? Cron might run under a different user which cannot access your /myfolder or /html dir, so make sure to give the users apropriate rights. You can see those two answers Answer1 | Answer2 I posted some time back on how to set up permissions.

Community
  • 1
  • 1
baao
  • 71,625
  • 17
  • 143
  • 203
  • I have set a mkdir command with the same path as where the zip file is, and the folder is created as expected - so I think permissions are ok. But I still cant get the cronjob to open and unzip the zip file. – R2D2 Aug 03 '15 at 16:07
  • Ok, what happens when you run it on the cli without cron? Simply `/usr/bin/php /full/path/to/file.php` – baao Aug 03 '15 at 16:15
1

Finally managed to get to the bottom of this, after much testing and investigating!

Thanks to all who gave suggestions, but it seemed that when I was running the script through a browser, it was running at PHP version 5.4, but when the cron job was running, it was running at version 4.9!

Changing my cronjob calling script from:

0 * * * * /usr/bin/php

to

0 * * * * /usr/bin/php5

solved the zip problem, and the cronjob now unzips my file correctly!

R2D2
  • 2,620
  • 4
  • 24
  • 46