R_User
R_User

Reputation: 11082

Calling gzip from PHP fails

I have a backup-script for my database that worked well until recently. Now I found out that compressing the MySQLdump (a 1.3GB file) fails.

<?php
echo "<pre>";
echo "Current time: ".date('Y-m-d _ H-i-s')."\n";

$state = 0;
$result = system('gzip "dumpDB - 2015-05-17 _ 12-01-31.sql"', $state);

echo "Current time: ".date('Y-m-d _ H-i-s')."\n";
echo "Result: ".$result."\n";
echo "State:  ".$state."\n";
echo "</pre>";

Results in

Current time: 2015-05-17 _ 12-12-40
Current time: 2015-05-17 _ 12-13-40
Result: 
State:  9

For me this looks like a timeout, since the execution always takes about a minute (but this might be coincidentally). Unfortunately I could not find the meaning of the value 9 in $state. When trying to unzip, I get the following message:

gzip: dumpDB - 2015-05-17 _ 12-01-31.sql.gz: unexpected end of file

When running the PHP-script form the terminal using php -f zip.php the script finishes with exit status 0 and the zip-file is OK:

Current time: 2015-05-17 _ 13-38-22
Current time: 2015-05-17 _ 13-40-11
Result: 
State:  0

So why is gzip aborted when running the PHP-script in my browser?

Upvotes: 3

Views: 177

Answers (1)

DoctorFox
DoctorFox

Reputation: 173

Your PHP CLI settings are different from the webserver settings mostly. In CLI mode your script could run for up to 300s without crashing. However, in the browser the default timeout is 30s.

You could bypass this temporarily by using

ini_set('max_execution_time', 0);

At the top of your script.

Also, the value 9 in the $state variable is the last line returned from the execution of the command. Might be better if you used passthru as per this comment on the PHP manual.

If you need to execute a command and have all the data from the command passed directly back without any interference, use the passthru() function.

PHP manual: System()

Upvotes: 2

Related Questions