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);
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 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');
Download multiple .txt files into .zip file with PHP
Yes you can download multiple text files as single zip file using php ZipArchive
class. This is sample only you can try with your own scenario.
$zipname = 'sample.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
$files_array = ["sample1", "sample2"]; // List of file names
foreach ($files_array as $file) {
$row = ["Line 1", "Line 2", "Line 3", "Line 5"]; // Assuming as your table row
$filename = $file.".txt";
$lines_to_add = "";
foreach(array_slice($row, 2) as $line) {
$lines_to_add .= $line."\r\n";
}
$zip->addFromString($filename, $lines_to_add); // Adding lines to file
}
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);
unlink($zipname); // Remove zip file if you don't want be in server
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.
Related Topics
How to Enable Curl in PHP/Xampp
How to Recursively Delete a Directory and Its Entire Contents (Files + Sub Dirs) in PHP
"Connect Failed: Access Denied For User 'Root'@'Localhost' (Using Password: Yes)" from PHP Function
Get PHP to Stop Replacing '.' Characters in $_Get or $_Post Arrays
Declaration of Methods Should Be Compatible With Parent Methods in PHP
How to Solve "Fatal Error: Class 'MySQLi' Not Found"
How to Pass Data to View in Laravel
Why Does MySQLi Num_Rows Always Return 0
Why Is the Hash Part of the Url Not Available on the Server Side
Set Up the Base Url in Codeigniter
How to Replace Microsoft-Encoded Quotes in PHP
Turning Multidimensional Array into One-Dimensional Array
Full Secure Image Upload Script
PHP Multidimensional Array Searching (Find Key by Specific Value)
How to Get the Base Url With PHP