Results 1 to 9 of 9

Thread: Multiple Forced Downloads

  1. #1
    Join Date
    Jun 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Multiple Forced Downloads

    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!

  2. #2
    Join Date
    Jun 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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?

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I think it's more likely to be your browser, personally. If not, then yes, there's something very wrong with your PHP setup.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Jun 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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?

    Code:
    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");
    }

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #6
    Join Date
    Jun 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by djr33
    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.

  7. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Have you tried this:

    PHP Code:
    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...
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Ah, true. Nevermind.



    Any luck?
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •