Is there any way to detect if a gz file is corrupt in PHP?
I'm currently using http://www.php.net/manual/de/function.gzread.php#110078 to determine the file size and read the whole* file via
$zd = gzopen ( $file, "r" );
$contents = gzread ( $zd, $fzip_size );
gzclose ( $zd );
Unfortunately some gz files are corrupted and the last 4 bytes do not represent the real length of the gz file. As long as the number is negativ I'm able to tell that something is wrong, but sometimes it's positive (and very large) which leads to an out of memory error. How can I check in advance if the file is corrupted?
If you can use linux gzip command it will be very simply to find if file is wrong or not. gzip -t will display no message if file is valid.
if (`gzip -t $file 2>&1`) {
echo "An error occured";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With