Symfony2 create and download zip file

php, symfony, zip

Solution

solved:

$zip->close();
header('Content-Type', 'application/zip');
header('Content-disposition: attachment; filename="' . $zipName . '"');
header('Content-Length: ' . filesize($zipName));
readfile($zipName);

apparently closing the file is important ;)

Problem

I have one application that upload some files and then I can compress as zip file and download. The export action: ``` public function exportAction() { $files = array(); $em = $this->getDoctrine()->getManager(); $doc = $em->getRepository('AdminDocumentBundle:Document')->findAll(); foreach ($_POST as $p) { foreach ($doc as $d) { if ($d->getId() == $p) { array_push($files, "../web/".$d->getWebPath()); } } } $zip = new \ZipArchive(); $zipName = 'Documents-'.time().".zip"; $zip->open($zipName, \ZipArchive::CREATE); foreach ($files as $f) { $zip->addFromString(basename($f), file_get_contents($f)); } $response = new Response(); $response->setContent(readfile("../web/".$zipName)); $response->headers->set('Content-Type', 'application/zip'); $response->header('Content-disposition: attachment; filename=../web/"'.$zipName.'"'); $response->header('Content-Length: ' . filesize("../web/" . $zipName)); $response->readfile("../web/" . $zipName); return $response; } ``` everything is ok until the line header. and everytime I'm going here I got the error: "Warning: readfile(../web/Documents-1385648213.zip): failed to open stream: No such file or directory" What is wrong? and why when I upload the files, this files have root permissions, and the same happens for the zip file that I create.

Original source