2

The PHP ZipArchive class will let me create a zip archive with the open() method:

mixed ZipArchive::open ( string $filename [, int $flags ] )

I can then add files to this archive, close the file, and download it into the user's web browser as per usual:

header ('Content-Type: application/zip');
header ('Content-Length: ' . filesize ($filename));
header ('Content-Disposition: attachment; filename="' . $filename . '"');
readfile ($filename);
unlink ($filename);

My question: is there a way to avoid creating a file on the server harddisk, downloading that and then erasing it? I.e. is it possible to use stdout as the target filename passed to the open() method and write the archive contents to stdout directly? Something along the lines of:

ZipArchive::open ( STDOUT, ZipArchive::CREATE)

Or am I stuck with the necessity for an intermediate server-side file?

  • 1
    Just wondering, why are you opposed to the idea of a server-side file? Even if this on-the-fly method is possible, it would be either via temp files in the background (which gets you back to square one) or using server RAM (which can be a separate kind of hell, depending on the server usage and the size/amount of the files). – Sergey Vidusov Feb 18 '16 at 07:23
  • I'm running in an environment with severe disk space and write access restrictions. However your point on server RAM usage is well taken. – Frank van Wensveen Feb 19 '16 at 16:54

1 Answers1

2

"ZipStream is a library for dynamically streaming dynamic zip files from PHP without writing to the disk at all on the server." Link: https://github.com/maennchen/ZipStream-PHP

codemonkey
  • 7,325
  • 5
  • 22
  • 36