tags:

views:

41

answers:

2

I have a PHP page that sends a file to the browser depending on the request data it receives. getfile.php?get=something sends file A, getfile.php?get=somethingelse sends file B and so on and so forth. This is done like so:

header('Content-Disposition: attachment; filename='. urlencode($filename));
readfile($fileURL);

It works except it can only send one file at a time. Any other files requested are send in linear fashion. One starts as soon as another finishes.

How can I get this to send files in parallel if the user requests another file while one is downloading?

Edit: I have tried downloading two files at the same time by directly using their filepaths and it works, so neither Apache, nor the browser seem to have a problem. It seems PHP is the issue. I have by the way used session_start() at the beginning of the page.

+1  A: 

This may be down to their browser settings, your server settings, PHP, or all three. Most browsers will only process two simultaneous HTTP connections to the same server, queuing others. Many web servers will also queue connections if there are more than two from the same browser. If you're using sessions, PHP may be serializing fulfilling requests in the session (to just one active request at a time) to minimize race conditions. (I don't know if PHP does this; some others do.)

Two of these (server and PHP) you're in control of; not much you can do about the browser.

Somewhat OT, but you could always allow them to select multiple files and then send them back a dynamically-created zip (or other container format).

T.J. Crowder
A: 

Adding session_write_close() right after I'm finished with the session and before starting the download seems to have solved the issue.

Manos Dilaverakis