我修改了 thomas dot rubbert at yahoo dot de 的代码
$uploads_dir =storage_path().'/app/public/files/' ;
if(!file_exists($uploads_dir)){
mkdir($uploads_dir,0777,true);
}
$zip_file=$uploads_dir.'download.zip';
if(file_exists($zip_file)){
unlink($zip_file);
}
$zip = new \ZipArchive();
if (!$zip->open($zip_file, \ZIPARCHIVE::CREATE)) {
http_response_code(501);
echo 'Zip creation failed';
die();
}
$img=$uploads_dir.'img';
$dirName = realpath($img);
if (substr($dirName, -1) != DIRECTORY_SEPARATOR) {
$dirName.= DIRECTORY_SEPARATOR;
}
$dirStack = array($dirName);
//Find the index where the last dir starts
$cutFrom = strrpos(substr($dirName, 0, -1), DIRECTORY_SEPARATOR)+1;
while (!empty($dirStack)) {
$currentDir = array_pop($dirStack);
$filesToAdd = array();
$dir = dir($currentDir);
while (false !== ($node = $dir->read())) {
if (($node == '..') || ($node == '.')) {
continue;
}
if (is_dir($currentDir . $node)) {
array_push($dirStack, $currentDir . $node . DIRECTORY_SEPARATOR);
}
if (is_file($currentDir . $node)) {
$filesToAdd[] = $node;
}
}
$localDir =str_replace(DIRECTORY_SEPARATOR,'/',substr($currentDir, $cutFrom));
$zip->addEmptyDir($localDir);
foreach ($filesToAdd as $file) {
$zip->addFile($currentDir . $file, $localDir . $file);
}
}
// any additional files
$db=$uploads_dir.'databse.db';
//$zip->addFromString(basename($db), file_get_contents($db));
$zip->addFile($db, basename($db));
$zip->close();