创建zip时更改存储文件夹

时间:2013-09-03 21:59:54

标签: php

我使用此代码从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)

1 个答案:

答案 0 :(得分:1)

我已经用这种方式解决了:

$new_zipname = substr($zipname,strrpos($zipname,'/') + 1);
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$new_zipname);