Downloading Large File with Speed Limit

Hi Guys,

I’m writing a file hosting script, my problem is when the download begin, the site wont be able to access by this user until the download process is finish, any idea how could this happen?

The user still able to browse the page via another browser. but not with the current one which downloading the file.

These are my downloading code.




$file = fopen($local_file, "rb");

while (!($user_aborted = connection_aborted() || connection_status() == 1) && $size>0) {

                  $startpacket = microtime(1);

                  //$bandw = round($download_rate * 1024);

                  $bandw = $download_rate * 1024;

		  if($size<$bandw)

                    $bandw = $size;

                  // send the current file part to the browser

                  echo fread($file, $bandw);

                  ob_flush();

                  flush();

           

                  // sleep one second

                  usleep(1000000);

                  

                        

                  if($i%60==0){

                   $this->filedownload->touch = date("Y-m-d G:i:s");

                   $this->filedownload->save();

                   $newf = Download::model()->findByPk($this->filedownload->id);

                   if($newf->ko==true)

                      break;

                  }                 

                 $i++;

                 $size -= $bandw;

              }



Hi, welcome to the forum.

If you use file based sessions, then a session for a user will block until the request is complete - example:

You could switch to db sessions for example, they don’t block.

Also instead of sending out the files via php, you should better let the webserver handle it using the x-sendfile feature which supports rate limiting:

http://www.yiiframework.com/doc/api/1.1/CHttpRequest#xSendFile-detail (<- contains links to popular webservers with implementation details)

You may want to checkout a nice piece of code provided by mindplay: https://gist.github.com/1555496 I am sure you will use lots of things in your work

Thanks for the clue. i solve with just a line of code,


session_write_close();

I did look into the xsend, but my issue is different login will have different speed limit for download, and each finished download only will +1 count to the table in db.

If with xsend, we cannot know for sure whether the file finish download or not, but we just can know when it start.

Thanks for reply. For our server performance, we try to limit the user from concurrently download, so for this stage, we wont allowed partial content/ multithreaded download. Maybe later we will reconsider this method.