php - ZIP all files in directory and download .zip generated -
well, first of all, folder structure:
images/ image1.png image11.png image111.png image223.png generate_zip.php
and mine generate_zip.php:
<?php $files = array($listfiles); $zipname = 'adcs.zip'; $zip = new ziparchive; $zip->open($zipname, ziparchive::create); foreach ($files $file) { $zip->addfile($file); } $zip->close(); header('content-type: application/zip'); header("content-disposition: attachment; filename='adcs.zip'"); header('content-length: ' . filesize($zipname)); header("location: adcs.zip"); ?>
how gather files "images/" folder, except "generate_zip.php", , make downloadable .zip? in case "images/" folder have different image. possible?
this ensure file .php extension not added:
foreach ($files $file) { if(!strstr($file,'.php')) $zip->addfile($file); }
edit: here's 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"); ?>
Comments
Post a Comment