Download multiple files as a zip-file using php
You can use the ZipArchive
class to create a ZIP file and stream it to the client. Something like:
$files = array('readme.txt', 'test.html', 'image.gif');
$zipname = 'file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
and to stream it:
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);
The second line forces the browser to present a download box to the user and prompts the name filename.zip. The third line is optional but certain (mainly older) browsers have issues in certain cases without the content size being specified.
Download multiple files as a zip folder using php
you can take a look at ZipArchive
, you would be able to create zips with that and let the user download it.
Cletus provide a really good answer there. I humbly copy his sample here
$files = array('readme.txt', 'test.html', 'image.gif');
$zip = new ZipArchive;
$zip->open('file.zip', ZipArchive::CREATE);
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
and to stream it:
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename=filename.zip');
header('Content-Length: ' . filesize($zipfilename));
readfile($zipname);
ZIP all files in directory and download generated .zip
this will ensure a file with .php extension will not be added:
foreach ($files as $file) {
if(!strstr($file,'.php')) $zip->addFile($file);
}
edit: here's the full code rewritten:
<?php
$zipname = 'adcs.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
if ($handle = opendir('.')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != ".." && !strstr($entry,'.php')) {
$zip->addFile($entry);
}
}
closedir($handle);
}
$zip->close();
header('Content-Type: application/zip');
header("Content-Disposition: attachment; filename='adcs.zip'");
header('Content-Length: ' . filesize($zipname));
header("Location: adcs.zip");
?>
Download multiple images into zip
You should download external files and then archive them.
$image1 = "http://cdn.screenrant.com/wp-content/uploads/Darth-Vader-voiced-by-Arnold-Schwarzenegger.jpg";
$image2 = "http://cdn.screenrant.com/wp-content/uploads/Star-Wars-Logo-Art.jpg";
$files = array($image1, $image2);
$tmpFile = tempnam('/tmp', '');
$zip = new ZipArchive;
$zip->open($tmpFile, ZipArchive::CREATE);
foreach ($files as $file) {
// download file
$fileContent = file_get_contents($file);
$zip->addFromString(basename($file), $fileContent);
}
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename=file.zip');
header('Content-Length: ' . filesize($tmpFile));
readfile($tmpFile);
unlink($tmpFile);
In example above I used file_get_contents
function, so please enable allow_url_fopen
or use curl to download the files.
Download multiple files as a ZIP - Laravel 7
ah.. Nevermind, found my owned issue.
$zipFile = public_path().'/assets/fe/img/portfolio/'.$folderName.'/'.$folderName.'.zip';
Working Code
public function download($id)
{
$portfolioImages = PortfolioImage::where('portfolio_id',$id)->get();
$files = [];
foreach ($portfolioImages as $i => $portfolioImage) {
$files[$portfolioImage->id] = public_path(). $portfolioImage->image_path;
}
// dd($files);
$portfolio = Portfolio::find($id);
$folderName = $portfolio->id.'-'.str_replace(' ', '-',$portfolio->name);
$zip = new ZipArchive;
$zipFile = public_path().'/assets/fe/img/portfolio/'.$folderName.'/'.$folderName.'.zip';
if ($zip->open($zipFile, ZipArchive::CREATE) === TRUE)
{
//add files into a zip
foreach ($files as $key => $value) {
//replace word "full" with $portfolioImage->id
$relativeNameInZipFile = str_replace('full',$key,basename($value));
$zip->addFile($value, $relativeNameInZipFile);
}
$zip->close();
}
return response()->download($zipFile);
}
Result ✨
zip and download files using php
Thanks for your answers.
<?php
$files = array('Dear GP.docx','ecommerce.doc');
# create new zip opbject
$zip = new ZipArchive();
# create a temp file & open it
$tmp_file = tempnam('.','');
$zip->open($tmp_file, ZipArchive::CREATE);
# loop through each file
foreach($files as $file){
# download file
$download_file = file_get_contents($file);
#add it to the zip
$zip->addFromString(basename($file),$download_file);
}
# close zip
$zip->close();
# send the file to the browser as a download
header('Content-disposition: attachment; filename=Resumes.zip');
header('Content-type: application/zip');
readfile($tmp_file);
?>
Zip multiple files and download using php codeigniter
I found out the solution. All I have to do is add ob_start(); in the open of the controller file.
ob_start();
$dataZip = array(
'./downloads/fda.in' => $fda,
'./downloads/fwl.in' => $fwl
);
$this->zip->add_data($dataZip);
$this->zip->archive('./downloads/files_backup.zip');
$this->zip->download('files_backup.zip');
Related Topics
Get the Selected Index Value of <Select> Tag in PHP
How to Prevent Self Closing Tag in PHP Simplexml
How to Connect to the Database in Openshift Application
How to Pass Variables Between PHP Scripts
I Want a Pagination to My Options Page of Wordpress Plugin
Pass Form Data to Another Page with PHP
Google Drive API - PHP Client Library - Setting Uploadtype to Resumable Upload
Getting All $_Post from Multiple Select Value
How to Get the Shortest Rather Than Longest Possible Regex Match with Preg_Match()
Mysql: Determine Table's Primary Key Dynamically
Is Using Superglobals Directly Good or Bad in PHP
Why Doesn't PHP Dom Include Slash on Self Closing Tags
How to Get a List of Databases
Gmail API: 400 Bad Request When Trying to Send an Email (PHP Code)
Get Number of Weekdays in a Given Month