2015-01-09 112 views

回答

1

此功能读写压缩小块,你可以使用这个功能来压缩大文件

 

/** 
* @return bool 
* @param string $source 
* @param string $dest 
* @desc compressing the file with the zlib-extension 
*/ 
function gzipCompress($source, $dest, $level = 5){ 

    if($dest == false){ 
     $dest = $source.".gz"; 

    } 

    if(file_exists($source)){ 

     $filesize = filesize($source); 
     $source_handle = fopen($source, "r"); 

     if(!file_exists($dest)){ 

      $dest_handle = gzopen($dest, "w$level"); 

      while(!feof($source_handle)){ 

       $chunk = fread($source_handle, 2048); 
       gzwrite($dest_handle, $chunk); 

      } 

      fclose($source_handle); 
      gzclose($dest_handle); 

      return true; 

     } else { 

      error_log("The $dest already exists"); 

     } 

    } else { 

     error_log("The $source does not exist."); 

    } 

    return false; 
} 

要enbable在php.ini文件zlib压缩查找要用zlib.output_compression在您的服务器,并从0改变其值为1.您还可以通过更改zlib.output_compression_level的值来更改压缩级别,并且存在zlib.output_handler以更改输出句柄。

+0

嗨,谢谢你回答抱歉我的问题可能写错了。我想为我的网站启用Zlip压缩。对于页面速度 –

+0

这是我发现的。告诉我编辑php.ini文件并添加它。 --with-zlib [= DIR] zlib.output_compression = On –