View Full Version : Multiple Forced Downloads
PixelNurse
06-27-2006, 10:35 AM
Hey Guys,
I'm having an odd problem and I was wondering if anybody knew of a solution or could tell me what's going on...
I have a page where there are multiple files the user need to download. Each of the file links goes to a PHP page which has a GET parameter passed to it. The parameter is then used to identify the file to download. It reads out a file from outside the webroot and forces it to download. That's working great, however there is one problem. Once the user clicks one link the file starts to download, however if they try to click another download link while the first file is still transferring it doesn't start to download until the first file has finished.
Is it not possible for PHP to serve multiple files at once? Just to be clear I'm not trying to have one processed page serve multiple files, but have the same page be hit multiple times each time serving out a different file.
I'd really appreciate any thoughts you guys had as this is really got me stumped and I'm having a devil of a time trying to get anything out of google on this issue.
Thanks in advance for any thoughts!
PixelNurse
06-27-2006, 11:16 AM
OK clearly something much broader is going on as it's not just the other files that can't be download, but no other PHP page will be served until the file download has completed. Can something be wrong with my PHP configuration or is it likely to be my file download that's doing something evil?
I think it's more likely to be your browser, personally. If not, then yes, there's something very wrong with your PHP setup.
PixelNurse
06-27-2006, 12:49 PM
Thanks for the suggestion but it's definitely not the browser. I can download multiple files from the server at once if i access them directly, but not via the PHP page.
Here's the relevant bit of the code handling the file download, can anyone see what it might be?
header("HTTP/1.1 200 OK");
header("Content-Length: $fsize");
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=\"$nicename\";");
header("Content-Transfer-Encoding: binary");
if(file_exists($fpath) && $fh = fopen($fpath, "rb")){
while($buf = fread($fh, $bufsize)) {
print $buf;
fclose($fh);
}
} else {
header("HTTP/1.1 404 Not Found");
}
djr33
06-27-2006, 08:21 PM
Your filesize is outputted and never assigned a value.
The filesize should be output AFTER the file is opened, checked, etc.
The if should include the header output.
You wouldn't want the header output to be for a force-download if you're going to then output a 404 either.
PixelNurse
06-27-2006, 09:58 PM
Your filesize is outputted and never assigned a value.
The filesize should be output AFTER the file is opened, checked, etc.
The if should include the header output.
You wouldn't want the header output to be for a force-download if you're going to then output a 404 either.
There is setting of all the variables I'm using beforehand, there's also various database processes, sessions, checking for the existence of the file, etc. I just posted the bit that causes the problem. I've tried all manner of different headers too, without the size also, and apart from obviously the browser doesn't know when the file is gonna end, the same problem occurs.
djr33
06-28-2006, 06:24 AM
Have you tried this:
if(file_exists($fpath) && $fh = fopen($fpath, "rb")){
while($buf = fread($fh, $bufsize)) {
header("HTTP/1.1 200 OK");
header("Content-Length: $fsize");
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=\"$nicename\";");
header("Content-Transfer-Encoding: binary");
print $buf;
fclose($fh);
}
} else {
header("HTTP/1.1 404 Not Found");
}
If not, I say try, see if it works. (maybe print $buf should be removed if it's setup like that... I don't see where $buf is set, so wouldn't know)
If it doesn't, which is likely if you've tried similar things before, then we'll probably need to see the rest of the code (passwords, etc. removed).
But... ok, looking more closely at your problem, here's what I'm thinking:
Isn't the script doing something with the file? It's storing the file in memory as it does stuff, including sending the file. Might it not want to do two at a time? The server is busy holding the one in memory and not wanting to overload.
this might be totally off, though.
Aside from that, I don't really know. Twey knows more about this stuff than I do...
That script will send headers again after each chunk of the file, which really wouldn't work out. The headers should be outside the while loop.
djr33
06-29-2006, 06:58 AM
Ah, true. Nevermind.
Any luck?
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.