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?

3
  • What is $localpath and $localzipfile? Commented Aug 3, 2015 at 12:31
  • code updated to include these Commented Aug 3, 2015 at 14:09
  • Can you please try to do what I suggested in the linked Answer1 below (mkdir() in both directories) with nothing else in the file beeing run by cron Commented Aug 3, 2015 at 14:19

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

$localzipfile = "daily/newzipfile.zip";
$localpath = pathinfo(realpath($localzipfile), PATHINFO_DIRNAME);
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 ?
I have updated my code and question with full path to $localzipfile, but code still isn't unzipping the file when it runs.
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.

2 Comments

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.
Ok, what happens when you run it on the cli without cron? Simply /usr/bin/php /full/path/to/file.php
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!

Comments

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.