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
Categories:
php
View Comments
Comment or Ask a Question
Quick Links