我正在使用 symfony 2.0,我正在尝试动态生成一个 zip。
这是我的代码:
public function downloadFontAction($slug)
{
$font = $this->getRepository(static::FONT_REP)->findOneBySlug($slug);
$zip = new \ZipArchive;
$zipName = $slug.".zip";
if ($zip->open($zipName, \ZIPARCHIVE::CREATE | \ZIPARCHIVE::OVERWRITE) === TRUE) {
foreach($this->container->getParameter('font_extensions') as $ext)
{
$fontFile = call_user_func(array($font, "get".ucfirst($ext)));
//adding Fonts
if ($fontFile)
{
$zip->addFile('/Users/admin/Documents/public_html/GitHub/typ/web/static/'.$fontFile,$font->getSlug().".".$ext);
}
}
$zip->close();
} else {
echo 'failed';
}
$response = new Response();
//$response->setContent(readfile($zipName));
$response->setStatusCode(200);
$response->headers->set('Content-Type', 'application/zip');
$response->headers->set('Content-Disposition', 'attachment; filename="'.$zipName.'"');
$response->headers->set('Content-Transfer-Encoding', 'binary');
$response->headers->set('Content-Length', filesize($zipName));
$response->headers->set('Pragma', 'public');
$response->headers->set('Expires', '0');
$response->headers->set('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');
$response->headers->set('Cache-Control', 'private');
@unlink($zipName);
return $response;
}
该函数返回一个空的 zip 文件,一旦我解压缩该文件,就会出现一个执行无限 bucle 的 cpgz 文件。zip 在服务器端正确生成,但无法通过此操作下载。