Download Multiple Files as a Zip Folder Using PHP

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 ✨

Sample Image

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



Leave a reply



Submit