Gzip call from PHP fails
I have a backup script for my database that worked well until recently. Now I found out that MySQLdump (a 1.3GB
) file compression is failing .
<?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
It looks like a timeout to me, as the execution always takes about a minute (but it could be random). Unfortunately, I couldn't find the value of the value 9
in $state
. When I try to unzip, I get the following message:
gzip: dumpDB - 2015-05-17 _ 12-01-31.sql.gz: unexpected end of file
When running a PHP-script form, the terminal using the php -f zip.php
script ends up with an 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 PHP-script in my browser?
source to share
PHP CLI settings are mostly different from web server settings. In CLI mode, your script can run for up to 300s without crashing. However, in the browser, the default timeout is 30s.
You can work around this temporarily using
ini_set('max_execution_time', 0);
At the top of your script.
In addition, the value 9 in the $ state variable is the last line returned from the command execution. It might be better if you used passthru as per this PHP manual comment.
If you need to execute a command and all data from a command passed directly without any hindrance, use the passthru () function.
source to share