Webkul
Services
Technologies Bagisto QloApps
iOS Android SAP BigCommerce Wix
Trusted by
Samsung Intel Toyota Walmart Disney Nokia Nestlé Decathlon
See all customers
Back to Top
DOC

How to add files to a ZIP and download it in PHP

Hello,

Here we learn, how to add files to a zip and download that zip in PHP.

For Example:

We have an array of files:

$files =array("test.txt", "demo.jpg", "hello.txt", "example.txt");// array of files

 $zip = new ZipArchive();//Create an object of ZipArchive class
  
 $zipname="example_zip";// give zip file name 

 $zip->open($zipname, ZipArchive::CREATE); //example_zip zip file created 
 
 foreach($files as $key =>$file){
     $zip->addFile($file);//add each file into example_zip zip file
 }
 
 $zip->close();// zip file with files created successful now close it

Download Zip File
  header('Content-Type: application/zip');
  header('Content-disposition: attachment; filename='.$zipname);
  header('Content-Length: ' . filesize($zipname));
  readfile($zipname);

 

 

Thanks

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home