php 壓縮檔為zip格式的函數代碼

來源:互聯網
上載者:User
  1. /* @creates a compressed zip file 將多個檔案壓縮成一個zip檔案的函數
  2. * @$files 數群組類型 執行個體array("1.jpg","2.jpg");
  3. * @destination 目標檔案的路徑 如"c:/androidyue.zip"
  4. * @$overwrite 是否為覆蓋與目標檔案相同的檔案
  5. * @site http://bbs.it-home.org
  6. */
  7. function create_zip($files = array(),$destination = '',$overwrite = false) {
  8. //if the zip file already exists and overwrite is false, return false
  9. //如果zip檔案已經存在並且設定為不重寫返回false
  10. if(file_exists($destination) && !$overwrite) { return false; }
  11. //vars
  12. $valid_files = array();
  13. //if files were passed in...
  14. //擷取到真實有效檔案名稱
  15. if(is_array($files)) {
  16. //cycle through each file
  17. foreach($files as $file) {
  18. //make sure the file exists
  19. if(file_exists($file)) {
  20. $valid_files[] = $file;
  21. }
  22. }
  23. }
  24. //if we have good files...
  25. //如果存在真實有效檔案
  26. if(count($valid_files)) {
  27. //create the archive
  28. $zip = new ZipArchive();
  29. //開啟檔案 如果檔案已經存在則覆蓋,如果沒有則建立
  30. if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
  31. return false;
  32. }
  33. //add the files
  34. //向壓縮檔中添加檔案
  35. foreach($valid_files as $file) {
  36. $zip->addFile($file,$file);
  37. }
  38. //debug
  39. //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
  40. //close the zip -- done!
  41. //關閉檔案
  42. $zip->close();
  43. //check to make sure the file exists
  44. //檢測檔案是否存在
  45. return file_exists($destination);
  46. }else{
  47. //如果沒有真實有效檔案返回false
  48. return false;
  49. }
  50. }
  51. /****
  52. //測試函數
  53. $files=array('temp.php','test.php');
  54. create_zip($files, 'myzipfile.zip', true);
  55. ****/
  56. ?>
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.