2015-01-09 117 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 –