php生成gz文件,如何使用PHP创建.gz文件?
這里的其他答案在壓縮期間將整個文件加載到內存,這將導致大內存文件的“內存不足”錯誤。下面的函數應該在大文件上更可靠,因為它以512kb的塊讀取和寫入文件。
/**
* GZIPs a file on disk (appending .gz to the name)
*
* From http://stackoverflow.com/questions/6073397/how-do-you-create-a-gz-file-using-php
* Based on function by Kioob at:
* http://www.php.net/manual/en/function.gzwrite.php#34955
*
* @param string $source Path to file that should be compressed
* @param integer $level GZIP compression level (default: 9)
* @return string New filename (with .gz appended) if success, or false if operation fails
*/
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;
}
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的php生成gz文件,如何使用PHP创建.gz文件?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: modern php怎么网,Modern
- 下一篇: 动态规划算法php,php算法学习之动态