2011-11-22 23:29

[PHP] 把多個圖片壓縮封裝並下載

  1. <?php 
  2. $fileList = array( 
  3.    '/var/www/pics/001.jpg', 
  4.    '/var/www/pics/002.jpg', 
  5.    '/var/www/pics/003.jpg', 
  6.    '/var/www/pics/004.jpg', 
  7.    '/var/www/pics/005.jpg', 
  8.    '/var/www/pics/006.jpg', 
  9. ); 
  10.  
  11.  
  12. /*建立臨時壓縮檔*/ 
  13. $file = tempnam("tmp", "zip"); 
  14. $zip = new ZipArchive; 
  15. $res = $zip->open($file, ZipArchive::CREATE|ZipArchive::OVERWRITE); 
  16. if ($res!==true) { exit('壓縮錯誤');} 
  17.  
  18. foreach ($filePathList as $filePath){ 
  19.    $zip->addFile($filePath, $fileName); 
  20. } 
  21. $zip->close(); 
  22.  
  23. ob_end_clean(); 
  24. header('Content-type: application/octet-stream'); 
  25. header('Content-Transfer-Encoding: Binary'); 
  26. header('Content-disposition: attachment; filename=pics_list.zip'); 
  27.  
  28. readfile($file); 
  29. unlink($file);  
  30. exit; 

參考連結:PHP: ZipArchive - Manual

0 回應: