How to compress a file say xxx.xml of 6MB into xxx.xml.gz using php program. How do you create a .gz file using PHP?
This solution doesn't work for me. it produce one .gz file which cant be open. it should be not just .gz it should be xxx.xml.gz
$source='xxx.xml'; function gzCompressFile($source, $level = 9){ $dest = $source . '.gz'; $mode = 'wb' . $level; $error = false; if ($fp_out = gzopen($dest, $mode)) { if ($fp_in = fopen($source,'rb')) { while (!feof($fp_in)) gzwrite($fp_out, fread($fp_in, 1024 * 512)); fclose($fp_in); } else { $error = true; } gzclose($fp_out); } else { $error = true; } if ($error) return false; else return $dest; } /////////////////////////////////////////////////////////////compresion of output file ends here //call gzCompressFile function starts gzCompressFile(); // call gzCompressFile function ends The other solution below is only for string not for file.
<?php $string = 'Some information to compress'; $gz = gzopen('somefile.gz','w9'); gzwrite($gz, $string); gzclose($gz); ?> Above one is for only string compression not for file compression Kindly help me to fix it.
gzCompressFile