I've an issue when downloading a file on my heroku app. I've a basic PHP ini wich allow us to have 128MB, for memory limit.
My Error is as follow :
(1/1) OutOfMemoryException Error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 77709312 bytes)
Ok I got it, but the weird thing is 77709312bytes
are only 74MB.
So what's wrong ? This a the code of my download action :
/**
* @Route("/templates/{id}/download", name="api_templates_download", requirements={"id"="\d+"})
* @Method({"GET"})
* @param \Symfony\Component\HttpFoundation\Request $request
* @return \Symfony\Component\HttpFoundation\Response
* @throws \Exception
*/
public function downloadTemplate(Request $request)
{
$id = $request->get('id');
try {
$template = $this->service->getTemplate($id, $this->helper->getUser());
$archive = $this->service->downloadTemplate($template);
$response = new Response(file_get_contents($archive));
$response->headers->set('Content-Type', 'application/zip');
$response->headers->set('Content-Disposition', 'attachment;filename="' . $archive . '"');
$response->headers->set('Content-length', filesize($archive));
return $response;
} catch (\Exception $e) {
$response = new Response(json_encode($e->getMessage()), 401);
$response->headers->set('Content-Type', 'application/json');
return $response;
}
}
And the method downloadTemplate
called in the controller :
/**
* @tests Need to tests about performance - do this in september with real server
* @param \App\Domain\Dollycast\Template\Entity\Template $template
* @return string
*/
public function downloadTemplate(Template $template)
{
$zip = new \ZipArchive();
$zipName = self::ZIPDIR . $template->getName() . '.zip';
$zip->open($zipName, \ZipArchive::CREATE);
$finder = new Finder();
$finder->files()->in(self::WORKDIR . $template->getName());
$zip->addEmptyDir($template->getName());
/** @var SplFileInfo $file */
foreach ($finder as $file) {
// Rename the full path to the relative Path
$zip->addFile(
self::WORKDIR . $template->getName() . DIRECTORY_SEPARATOR . $file->getRelativePathname(),
$template->getName() . DIRECTORY_SEPARATOR . $file->getRelativePathname()
);
}
$zip->close();
return $zipName;
}