www.webdeveloper.com
Results 1 to 9 of 9

Thread: [RESOLVED] PHP - Not allowing multiple downloads at the same time

  1. #1
    Join Date
    Oct 2008
    Posts
    51

    resolved [RESOLVED] PHP - Not allowing multiple downloads at the same time

    Hello all,

    Iíll try to make this as short as I can.

    First off, I donít know much about PHP so I purchased a couple of scripts online and Iíve been working with the author to try and find a solution to a problem I uncovered but we canít seem to find the cause or solution to the problem.

    These scripts have the same general purpose. They create long URLís to hide the true location of files that can be downloaded. Script A works fine. It basically uses sessions to create links to files so any user can download a file but still not know the true location of the file. Script B does the same thing but it uses cookies in addition to sessions and it also secures the page so only users of specific groups can access the page and the script generated links will only work for the specified group or specified user.

    The problem is that script B causes time outs and only one file can be downloaded at a time. Hereís how to reproduce the timeout:

    Say I have multiple links on a script B secured page and the user clicks on all of the links to start the download of all files at the same time. What happens is the first download starts and completes but the userís browser window goes blank and a PHP timeout error is returned and none of the other files start to download. Basically, the script will not allow multiple downloads to take place, at the same time, and if the first download is large enough, it causes the time out. If the files are really small, the timeout does not happen but the first download must complete before the second download starts and so on and so on.

    This does not happen with script A. The user can click on as many script A generated links as they want and all downloads start within seconds of each other and complete with no time outs no matter how large the files are and no matter how long it takes to download them.

    I hold the author of the scripts in very high regard. Both scripts use a lot of the same code and he seems to think the issue is internal to PHP since he can find no difference in the way both scripts handle downloads.

    I realize no one could possibly pinpoint the cause of this issue based on my description, but does anyone know if there is something internal to PHP that would cause one script to allow multiple downloads while the other only allows sequential downloads? Could it be related to cookies? My guess is that script B is still running in the background (for whatever reason) until the last download is completed.

    Thanks for taking the time to read my post

  2. #2
    Join Date
    Jan 2009
    Posts
    3,346
    How are the scripts handling actually outputting the file? fread? file_get_contents? Is output buffering used? Is flush() being called during the loop if fread is being used?

  3. #3
    Join Date
    Nov 2008
    Posts
    2,477
    Is the script using sessions?

  4. #4
    Join Date
    Oct 2008
    Posts
    51
    Quote Originally Posted by criterion9 View Post
    How are the scripts handling actually outputting the file? fread? file_get_contents? Is output buffering used? Is flush() being called during the loop if fread is being used?
    Thank you for replying.

    Since I don't know that much about PHP, the only way I can answer your questions is to perform word searches on the scripts.

    - I do see fread is being used.

    - Output buffering can be turned on and off or set to any size. I tried with different setting but it seemed to make no difference.

    - file_get_contents was not found in my search

    - I think I found a couple of instances when flush() is being called during a fread loop
    PHP Code:
    print(fread($file$sldownloadbuffer));
          
    ob_flush();
          
    flush();
          if (
    $sldownloadbuffer>10000)
            
    sleep(1);
       }
       
    fclose($file); 
    PHP Code:
    $mailBody fread($fh,100000);
        
    fclose($fh);
        
    $i=strrpos($template,".");
        
    $ext=substr($template,$i,strlen($template)-$i);
        
    $ext=strtolower($ext);
        
    // Get subject for email
        
    if ($ext==".txt")
        { 
    There are other numerous instances of fread that do not call flush().

    Thanks again.

  5. #5
    Join Date
    Oct 2008
    Posts
    51
    Quote Originally Posted by Mindzai View Post
    Is the script using sessions?
    Yes

  6. #6
    Join Date
    Nov 2008
    Posts
    2,477
    By default PHP gets a lock on the session file so it cannot be written to by a second instance of the session until the first is finished. I would look to do the actual file download before session_start is called, or after the session stuff has happened and session_write_close has been called.

  7. #7
    Join Date
    Jan 2009
    Posts
    3,346
    By default PHP gets a lock on the session file so it cannot be written to by a second instance of the session until the first is finished. I would look to do the actual file download before session_start is called, or after the session stuff has happened and session_write_close has been called.
    Good point Mindzai! I hadn't even thought of that (all my file download scripts I've built exist outside sessions).

  8. #8
    Join Date
    Oct 2008
    Posts
    51
    Thanks criterion9 and Mindzai!

    I'll pass your thoughts on to the author and see where that leads.

  9. #9
    Join Date
    Oct 2008
    Posts
    51
    Still testing, but adding session_write_close() after the "session stuff" seems to have solved the issue.

    Thanks guys!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles