I want to construct a zip and serve it back to the user. I currently use Symfony and so far I have the following which works well:
// Create zip.
$zip = new \ZipArchive();
$zip->open('/tmp/foo.zip', \ZipArchive::CREATE);
$zip->addFromString("test1.txt", "test 1 content");
$zip->addFromString("test2.txt", "test 2 content");
$zip->close();
// Read zip.
$stream = fopen('/tmp/foo.zip', 'r');
$zipData = fread($stream, 1024);
fclose($stream);
unlink('/tmp/foo.zip');
// Serve zip.
$response = new Response($zipData);
$response->headers->set('Content-Type', 'application/zip');
$response->headers->set('Content-Disposition', 'attachment; filename=foo.zip');
return $response;
My problem is that I want to avoid having to use the file system (/tmp/foo.zip
). I tried $zip->open('php://memory', ...)
which I thought that would do the job but it didn't work (result file was always 0 bytes). Is there a way to solve this?
Edit: I want to be using vanilla PHP and not directly make use of commands of the underlying O/S.