php compress files into zip files

WBOY
Release: 2016-07-25 08:43:16
Original
799 people have browsed it
PHP ZipArchive is an extension class that comes with PHP, which can easily compress and decompress ZIP files. Before using it, first make sure that the PHP ZIP extension is turned on.
  1. /* Description: Function to compress multiple files into one zip file
  2. * @param $files array type
  3. * @param destination path of the target file
  4. * @param $overwrite whether it is Overwrite the same file as the destination file
  5. */
  6. function create_zip($files = array(),$destination = '',$overwrite = false){
  7. //If the zip file already exists and is set to not overwrite, return false
  8. if(file_exists($destination) && !$overwrite) { return false; }
  9. $valid_files = array();
  10. //Get the real and valid file name
  11. if(is_array($files)) {
  12. foreach($ files as $file) {
  13. if(file_exists($file)) {
  14. $valid_files[] = $file;
  15. }
  16. }
  17. }
  18. //If there are real and valid files
  19. if(count($valid_files)) {
  20. $zip = new ZipArchive();
  21. //Open the file. If the file already exists, overwrite it, if not, create it.
  22. if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE ) !== true) { return false; }
  23. //Add files to the compressed file
  24. foreach($valid_files as $file) {
  25. $zip->addFile($file,$file);
  26. }
  27. // Close the file
  28. $zip->close();
  29. //Check whether the file exists
  30. return file_exists($destination);
  31. }else{
  32. return false;
  33. }
  34. }
  35. $files = array('tg. php');
  36. create_zip($files,'tg.zip', true);
  37. ?>
Copy code

File compression, php, zip


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