Rumah  >  Artikel  >  pembangunan bahagian belakang  >  PHP在线压缩zip的函数代码

PHP在线压缩zip的函数代码

WBOY
WBOYasal
2016-07-25 08:54:44810semak imbas
  1. /* PHP创建zip压缩包 */
  2. function create_zip($files = array(),$destination = '',$overwrite = false) {
  3. //if the zip file already exists and overwrite is false, return false
  4. if(file_exists($destination) && !$overwrite) { return false; }
  5. //vars
  6. $valid_files = array();
  7. //if files were passed in...
  8. if(is_array($files)) {
  9. //cycle through each file
  10. foreach($files as $file) {
  11. //make sure the file exists
  12. if(file_exists($file)) {
  13. $valid_files[] = $file;
  14. }
  15. }
  16. }
  17. //if we have good files...
  18. if(count($valid_files)) {
  19. //create the archive
  20. $zip = new ZipArchive();
  21. if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
  22. return false;
  23. }
  24. //add the files
  25. foreach($valid_files as $file) {
  26. $zip->addFile($file,$file);
  27. }
  28. //debug
  29. //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
  30. //close the zip -- done!
  31. $zip->close();
  32. //check to make sure the file exists
  33. return file_exists($destination);
  34. }
  35. else
  36. {
  37. return false;
  38. }
  39. }
  40. /***** 应用实例 ***/
  41. $files=array('1.php', 'mail.php', 'readme.txt');
  42. create_zip($files, 'myzipfile.zip', true);
  43. ?>
复制代码


Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn