SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: Limiting file speed

  1. #1
    Non-Member
    Join Date
    Jul 2006
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Limiting file speed

    Well, I took a function to read a file in chunks I noticed on php.net and concluded that I could limit the download speed if I cause the script to sleep after a chunk is read. I'm testing this on Windows XP Professional (WAMP5). The problem is, while the image file is being read, I can't load another page, it'll hang until the file is finished reading.
    PHP Code:
    function readfile_chunked($filename,$chunksize,$sleep=false,$retbytes=true) {
        
    $buffer '';
        
    $cnt =0;
        
    // $handle = fopen($filename, 'rb');
        
    $handle fopen($filename'rb');
        if (
    $handle === false) {
            return 
    false;
        }
        while (!
    feof($handle)) {
            
    $buffer fread($handle$chunksize);
            echo 
    $buffer;
            
    ob_flush();
            
    flush();
            if(
    $sleep){    
                
    sleep(1);
            }
            if (
    $retbytes) {
                
    $cnt += strlen($buffer);
            }
        }
        
    $status fclose($handle);
        if (
    $retbytes && $status) {
            return 
    $cnt// return num. bytes delivered like readfile() does.
        
    }
        return 
    $status;


    $DOWNLOAD_SPEED 10;
    $chunksize round($DOWNLOAD_SPEED*1024); //should limit the download to 10kib/s
    readfile_chunked($data['location'],$chunksize,true); 
    Any suggestions are welcome, even if you can't fix this script.

  2. #2
    SitePoint Wizard TheRedDevil's Avatar
    Join Date
    Sep 2004
    Location
    Norway
    Posts
    1,133
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Are there any specific reason you want to limit the upload speed? I say upload due to your server is uploading the file to the user, who again is downloading it.

    What you need to keep in mind, is that doing this will keep one process running until the file is downloaded. So if you have several users downloading something at the same time, then you might end up hogging all of the processes for a while giving any new users an error message saying there is no more available processes on your webpage.

    What bigger sites do, is run the download from a sub domain which again is limited by CoS (Class of Service) rules, these will decide what speed a download will receive. Handeling this on the php side, is just asking for problems in my opinion.

  3. #3
    Non-Member
    Join Date
    Jul 2006
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I apologize for my n00b. Thanks.

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
  •