I'm using symfony 2.0 and I'm trying to generate a zip dynamically.
This is my code:
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;
}
The function return an empty zip file that once I unzip the file appears a cpgz file doing a infinite bucle. The zip is correctly generated in the server side, but impossible to download through this action.
@unlink($zipName);
Are you delete the file before download it? maybe unlink doesn`t need here :)
I hope it is not too late. You are not setting the content of the response that is why it is an empty file. Befor returning the response, try something like this:
$response->setContent(file_get_contents($zipName));
Well same happened with me. I realized that it s permission problem while writing to the temporary folder.. try to give sufficient permission to write to the temp folder.
Plus try disabling gzip for the download script. I did mine by adding line below on top of php page.
ini_set('zlib.output_compression', 'Off');