0

我使用此代码从 PHP 创建 ZIP 文件 ZIP 已正确创建,但下载 zip 时遇到了一点麻烦。

在浏览器上,ZIP 名称如下所示

.._.._storage_temp_2013-09-03-1378245354.zip

我只想保持

2013-09-03-1378245354.zip

我在这里使用的代码:

$files = array($frente, $verso);
//$zipname = '../../storage/file.zip';
$zipname = "../../storage/temp/".date('Y-m-d')."-".time().".zip"; // Zip name

$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
    $new_filename = substr($file,strrpos($file,'/') + 1);
    $zip->addFile($file,$new_filename);
    //$zip->addFromString(basename($file),  file_get_contents($file));
}
$zip->close();

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

1 回答 1

1

我以这种方式解决了:

$new_zipname = substr($zipname,strrpos($zipname,'/') + 1);
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$new_zipname);
于 2013-09-03T22:09:23.547 回答