简体   繁体   中英

Download multiple files with php ZipArchive

I tried to use ZipArchive to download multiple files with this code recommended in this post

$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();

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

But it didn't work. Yes, it did create a zip-file but when I tried to extract it I got message that the archive is not readable and the archive may not be valid or its table of content could be encrypted. What's wrong?

I solved that the problem was the last 4 rows. Instead of that I used header('location: /'. $zipname); and it worked fine!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM