Script to download files only allows 1 download at a time?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gotts
    New Member
    • Feb 2007
    • 5

    Script to download files only allows 1 download at a time?

    Hi,
    I have a script which based on an id passed to the page is retrieving a filename and path from the database of a file and then outputting the file for download. I specifically did it this way (see the code below) so user would have a choice of "Saving" or "Running" the file.
    The problem is that with this method when a file is clicked for download the user cant then select another file to download. It seems like this script prevents the server from allowing more than one download at a time. I dont know much about this, so any help would be really appreciated!!!
    Thanks,
    here is the code which prompts the download
    [PHP]
    switch($extlwr) {
    case ($extlwr == ".zip"):
    $commonname="zi p";
    $ct = "Content-type: application/octet-stream";
    break;
    case ($extlwr == ".mp3"):
    $commonname="mp 3";
    $ct = "Content-type: audio/mp3";
    break;
    case ($extlwr == ".pdf"):
    $commonname="pd f";
    $ct = "Content-type: application/pdf";
    break;
    case ($extlwr == ".doc"):
    $commonname="do c";
    $ct = "Content-type: application/ms-word";
    break;

    }
    $size = strval(filesize ("$filepath$loc ation"));

    header("Pragma: public");
    header("Expires : 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private",false) ;
    header("$ct");
    header('Content-Disposition: attachment; filename="'.$fi lename.'";');
    header("Content-Transfer-Encoding: binary");
    if($extlwr != ".pdf"){
    header('Content-Length: '.$size);
    }
    readfile("$file path$location") ;
    exit;
    [/PHP]

    any ideas?
    thanks again
  • xwero
    New Member
    • Feb 2007
    • 99

    #2
    if you output the file to the browser the file is generated so there is nothing else to do but wait until the file is fully generated.

    You could save the files somewhere in a cache directory and display obfuscated for the download.

    you could also add

    [PHP]header("Content-Type: application/octet-stream");[/PHP]

    to the header to force the file to be downloaded.

    Comment

    • Gotts
      New Member
      • Feb 2007
      • 5

      #3
      Thanks for your help
      Even though I am outputting it to the browser, I am doing this in a new window. Why shoudl this stop the user from being able to click on another link in the original window to initiate a new download?

      What does this mean?
      "display obfuscated for the download."

      Thanks again

      Comment

      • xwero
        New Member
        • Feb 2007
        • 99

        #4
        Originally posted by Gotts
        Thanks for your help
        Even though I am outputting it to the browser, I am doing this in a new window. Why shoudl this stop the user from being able to click on another link in the original window to initiate a new download?

        What does this mean?
        "display obfuscated for the download."

        Thanks again
        I have no experience with it myself but have you tried a checkboxlist so that the user can choose which files he wants to download and then output the different files in new windows?


        Sorry it had to be display an obfuscated link for the download. Lets say a file is in the directory /upload/nice.jpg. You put in the download link something like download.php?fi le=fe4564ds4. the file parameter can be created from the session id and the directory of filename mixed in it in a pattern you know how to get it out again. This way you don't have to generate the files everytime someone wants to download them and you are protected from hot linking.

        Comment

        • Gotts
          New Member
          • Feb 2007
          • 5

          #5
          Originally posted by xwero
          I have no experience with it myself but have you tried a checkboxlist so that the user can choose which files he wants to download and then output the different files in new windows?


          Sorry it had to be display an obfuscated link for the download. Lets say a file is in the directory /upload/nice.jpg. You put in the download link something like download.php?fi le=fe4564ds4. the file parameter can be created from the session id and the directory of filename mixed in it in a pattern you know how to get it out again. This way you don't have to generate the files everytime someone wants to download them and you are protected from hot linking.
          I am sorry, I am still not clear. Why do I need to be generating files at all? in fact what does it mean to be generating files?
          I am using a download.php?fi d=24 where the fid is the fileid in the database so it knows which file to pull. I just dont know why when I output the file to browser it wont allow the user to download a different file. If the user clicks a different file then only once the first download completes does the second window popup asking the user where to save the file? In fact more than that - the whole website slows down for the user and they cant even browse to other pages on the site while a file is being downloaded?? There is obviously something i am doing very wrong here?
          thanks for your patience and help

          Comment

          • xwero
            New Member
            • Feb 2007
            • 99

            #6
            If you output the field content of the database it is one long string. To make it readable for humans the string is preceded by a header. this header tells the browser the file needs to be generated in that format. Then you add the content. So the file gets build from that string. Once it's finnished you see the image, can listen to the song, ...

            it's like a string you manipulate with string functions. but now php does the hard work for you.

            It is better to store the files in directories. This way no database connection is needed and the file already is in the human readable format.

            Because it's such a long string if the file is large it takes up all the server memory and you can't do anything else until the string is read out.

            Comment

            • Gotts
              New Member
              • Feb 2007
              • 5

              #7
              Originally posted by xwero
              If you output the field content of the database it is one long string. To make it readable for humans the string is preceded by a header. this header tells the browser the file needs to be generated in that format. Then you add the content. So the file gets build from that string. Once it's finnished you see the image, can listen to the song, ...

              it's like a string you manipulate with string functions. but now php does the hard work for you.

              It is better to store the files in directories. This way no database connection is needed and the file already is in the human readable format.

              Because it's such a long string if the file is large it takes up all the server memory and you can't do anything else until the string is read out.
              So would the following work, I think its similar to what you were explaining before,
              if someone clicks on a link a download script will copy the file from its source directory into a temporary directory with some temp filename and then serve that file to the user, not in the way I am serving it now with the headers and content but just by redirecting them to that file eg http://www.url.com/tempdir/tempfilename.mp 3
              Would that work? or do you have a better suggestion

              my only issue is that I would need to delete these temp files and when would I know to delete them?

              Comment

              • xwero
                New Member
                • Feb 2007
                • 99

                #8
                Originally posted by Gotts
                So would the following work, I think its similar to what you were explaining before,
                if someone clicks on a link a download script will copy the file from its source directory into a temporary directory with some temp filename and then serve that file to the user, not in the way I am serving it now with the headers and content but just by redirecting them to that file eg http://www.url.com/tempdir/tempfilename.mp 3
                Would that work? or do you have a better suggestion

                my only issue is that I would need to delete these temp files and when would I know to delete them?
                It's a way to go. To know how when to delete the picture you could put the date the file is clicked into a database table and temp file that are older then 30 minutes will be deleted.

                Comment

                • Gotts
                  New Member
                  • Feb 2007
                  • 5

                  #9
                  Originally posted by xwero
                  It's a way to go. To know how when to delete the picture you could put the date the file is clicked into a database table and temp file that are older then 30 minutes will be deleted.
                  actually I can just compare it to the time now, and more than 30 minutes delete it!
                  sounds good, thanks so much for your help!

                  Comment

                  • Hybridboy
                    New Member
                    • Sep 2007
                    • 1

                    #10
                    hey there!

                    I'm having the same problem. I'm using 2 servers though. One on which i have my php pages & one on which i put my mutlimedia...

                    I got the script to work, however, the site gets "stuck", until the download is complete...

                    Is there any way I could solve this problem please?

                    Comment

                    • bsprogs
                      New Member
                      • Jan 2007
                      • 6

                      #11
                      Originally posted by Hybridboy
                      hey there!

                      I'm having the same problem. I'm using 2 servers though. One on which i have my php pages & one on which i put my mutlimedia...

                      I got the script to work, however, the site gets "stuck", until the download is complete...

                      Is there any way I could solve this problem please?
                      I had this problem and it was a session header problem.
                      If you have "session_start( );" at the top of the page where you're initiating the download, it keeps the session header in use thus not allowing any other header information to be sent/received from that session.

                      If you remove session_start() ; from the top of the page, this should no longer be a problem.

                      Comment

                      Working...