gzip file using PHP
gzencode() function of PHP helps you create gzip file using php.
Open any file as shown in the below snippet, and read data and pass it to gzencode() function.
If the file to be gzip is of larger size, read file in chunks to avoid memory errors, while reading file.
More info on gzip can be found at PHP Manual in http://php.net
Tremendous compressing achieved with following snippet.
function gzipSitemap($fileIndex)
{
$file_to_gzip = “readme.txt”; //any type of file
$gz_dest_file = “readme.txt.gz”;
$fh = fopen($file_to_gzip, ‘r’) or die(“Can’t read file”);
$fp = fopen($gz_dest_file, “w”) or die(“Can’t create .gz file”);
while($theData = fread($fh, 5000))
{
$gzdata = gzencode($theData, 9);
fwrite($fp, $gzdata);
}
fclose($fp);
fclose($fh);
}
Leave a Reply