Why can PHP gzuncompress () function go wrong?

PHP has its own function for working with gzip archives. I wrote the following code:

error_reporting(E_ALL);
$f = file_get_contents('http://spiderbites.nytimes.com/sitemaps/www.nytimes.com/sitemap.xml.gz');
echo $f;
$f = gzuncompress($f);
echo "<hr>";
echo $f;

      

The first echo usually outputs a compressed file with the correct header (at least the first two bytes are correct). If I download this file using my browser, I can unpack it easily.

However gzuncompress threw Warning: gzuncompress(): data error in /home/path/to/script.php on line 5

Can anyone point me in the right direction to resolve this issue?

EDIT:

The phpinfo () output part

enter image description here

+3


source to share


2 answers


Or you can just use the correct function of decompression gzdecode()

.



+6


source


Note that gzuncompress () cannot decompress compressed strings and return a data error.

The problem might be that the external compressed string has a CRC32 checksum at the end of the file instead of Adler-32 as PHP expects.

( http://php.net/manual/en/function.gzuncompress.php#79042 )

This might be an option why it doesn't work.

Try his code:

function gzuncompress_crc32($data) {
     $f = tempnam('/tmp', 'gz_fix');
     file_put_contents($f, "\x1f\x8b\x08\x00\x00\x00\x00\x00" . $data);
     return file_get_contents('compress.zlib://' . $f);
}

      



Change the code as follows:

error_reporting(E_ALL);
$f = file_get_contents('http://spiderbites.nytimes.com/sitemaps/www.nytimes.com/sitemap.xml.gz');
echo $f;
$f = gzuncompress_crc32($f);
echo "<hr>";
echo $f;

      

As far as I tested locally, it no longer gives an error.

+5


source







All Articles