Простая реализация:
<?php
// base directory containing files that we're adding
$dir = 'images/';
// name of our zip file. best to use a unique name here
$zipfile = "test.zip";
// get a directory listing, remove self/parent directories, and reindex array
$files = array_values(array_diff(scandir($dir), array('.', '..')));
// form has been submitted
if (isset($_POST['submit'])) {
// initialize the zip file
$output = new ZipArchive();
$output->open($zipfile, ZIPARCHIVE::CREATE);
// add files to archive
foreach ($_POST['file'] as $num=>$file) {
// make sure the files are valid
if (is_file($dir . $file) && is_readable($dir . $file)) {
// add it to our zip file
$output->addFile($dir . $file);
}
}
// write zip file to filesystem
$output->close();
// direct user's browser to the zip file
header("Location: " . $zipfile);
exit();
} else {
// display filenames with checkboxes
echo '<form method="POST">' . PHP_EOL;
for ($x=0; $x<count($files); $x++) {
echo ' <input type="checkbox" name="file[' . $x . ']" value="' . $files[$x] . '">' . $files[$x] . '<br>' . PHP_EOL;
}
echo ' <input type="submit" name="submit" value="Submit">' . PHP_EOL;
echo '</form>' . PHP_EOL;
}
?>
Известная ошибка: не проверяет, существует ли $zipfile
заранее. Если это так, он будет добавлен к.