Search code examples
phpsymfonydownloadout-of-memorysymfony4

Download ZipArchive OutOfMemory


I've an issue when downloading a file on my heroku app. I've a basic PHP ini which allows us to have 128MB, for memory limit.

My Error is as follows:

(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;
}

Solution

  • file_get_contents will load the file into memory, when I put this into the Response object, this will effectively copy all the content from memory to the output buffer. This is why an archive wich is around 74MB will result in Out of memory for 128M configuration (74*2) error due to double work.

    At this time, I can't use readfile() to solve this problem because the behaviour is too much different for my needs. But readfile() will open and output into buffer without using twice memory.

    EDIT : The best alternative I found is by using the BinaryFileResponse object from Symfony 3.2 wich handle effectively the response stream.