tags:

views:

35

answers:

2

Hi,

I have a PHP script that creates a zip file on the fly and forces the browser to download the zip file. The question is: could I directly write the zip file to an output stream which is connected to the user's browser rather than save it as a real file on the server first and then send the file?

Thanks in advance.

A: 

If you're using the zip extension, the answer seems to be "no." The close method in the ZipArchive class is what triggers a write, and it seems to want to write to a file. You might have some luck using Streams to simulate a file, but then you're keeping the entire thing in memory, and you still couldn't send it until you're done adding files.

If you're having problems with timeouts or other problems while having a user wait for the zip file to be created, try a multi-step download process:

  1. User picks whatever they pick to create the zip
  2. They're taken to a status page that uses ajax calls to actually build the zip file in the background. A pretty, distracting animation should keep their attention.
  3. After the background processes build the zip, the user is redirected to a script that performs the download / redirected to the file.
Charles
A: 

No, you have to use a temporary file, as in here.

Artefacto