Home > Backend Development > PHP Tutorial > PHP compressed files are function codes in zip format

PHP compressed files are function codes in zip format

WBOY
Release: 2016-07-25 08:59:22
Original
976 people have browsed it
  1. /* @creates a compressed zip file Function to compress multiple files into one zip file
  2. * @$files array type instance array("1.jpg","2.jpg" );
  3. * @destination The path of the target file is such as "c:/androidyue.zip"
  4. * @$overwrite Whether to overwrite the same file as the target file
  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. //If the zip file already exists and overwrite is false, return false
  10. //If the zip file already exists and overwrite is set Return false for no rewriting
  11. if(file_exists($destination) && !$overwrite) { return false; }
  12. //vars
  13. $valid_files = array();
  14. //if files were passed in...
  15. // Get the real and valid file name
  16. if(is_array($files)) {
  17. //cycle through each file
  18. foreach($files as $file) {
  19. //make sure the file exists
  20. if(file_exists($file) ) {
  21. $valid_files[] = $file;
  22. }
  23. }
  24. }
  25. //if we have good files...
  26. //If there are real and valid files
  27. if(count($valid_files)) {
  28. // create the archive
  29. $zip = new ZipArchive();
  30. //Open the file. If the file already exists, overwrite it, if not, create it
  31. if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE ::CREATE) !== true) {
  32. return false;
  33. }
  34. //add the files
  35. //Add files to the compressed file
  36. foreach($valid_files as $file) {
  37. $zip->addFile($ file,$file);
  38. }
  39. //debug
  40. //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
  41. //close the zip -- done!
  42. //Close the file
  43. $zip->close();
  44. //check to make sure the file exists
  45. //Check whether the file exists
  46. return file_exists($destination);
  47. }else{
  48. //If there is no real and valid file, return false
  49. return false;
  50. }
  51. }
  52. /****
  53. //Test function
  54. $files=array('temp.php','test.php');
  55. create_zip($files, 'myzipfile.zip', true);
  56. ****/
?>
Copy code


🎜
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template