PHP: How to check if gz file is corrupted?

Is there a way to determine if the gz file is corrupted in PHP?

I am currently using http://www.php.net/manual/de/function.gzread.php#110078 to determine the file size and read the entire * file through

$zd = gzopen ( $file, "r" );
$contents = gzread ( $zd, $fzip_size );
gzclose ( $zd );

Unfortunately, some gz files are corrupt, and the last 4 bytes do not represent the actual length of the gz file. While the number is negative, I can say that something is wrong, but sometimes it is positive (and very large), which leads to an error from memory. How can I check in advance if the file is damaged?

  • I read the whole file because I did not find a working path for reading the file line by line, not knowing the size of the longest line, which led (in some cases) to lines that were not completed.
+4
source share
1 answer

If you can use the linux gzip command, it will be very easy to find if the file is incorrect or not. gzip -t will not display a message if the file is valid.

if (`gzip -t $file 2>&1`) {
    echo "An error occured";
}
0
source

Source: https://habr.com/ru/post/1525026/


All Articles