ZIParchive php返回cpgz
我正在使用symfony 2.0,并尝试动态生成一个zip 这是我的代码:ZIParchive php返回cpgz,php,symfony,zip,ziparchive,Php,Symfony,Zip,Ziparchive,我正在使用symfony 2.0,并尝试动态生成一个zip 这是我的代码: public function downloadFontAction($slug) { $font = $this->getRepository(static::FONT_REP)->findOneBySlug($slug); $zip = new \ZipArchive; $zipName = $slug.".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文件,解压后该文件显示为一个cpgz文件,执行无限次压缩。zip在服务器端正确生成,但无法通过此操作下载。@unlink($zipName)
您是否在下载之前删除该文件?
也许这里不需要取消链接:)我希望现在还不算太晚。您没有设置响应的内容,因此它是一个空文件。在返回响应之前,请尝试以下操作:
$response->setContent(file_get_contents($zipName));
我也是这样。在写入临时文件夹时,我意识到这是权限问题。。尝试给予足够的权限写入临时文件夹 另外,请尝试为下载脚本禁用gzip。我通过在php页面顶部添加下面的行完成了我的工作
ini_set('zlib.output_compression', 'Off');