Download Missing Last Chunk

Discussion in 'PHP' started by cesarcesar, Feb 9, 2011.

  1. #1
    I'm using the following script to download large file (>100Mb). It works well except that it seems to not ever save the final chunk. If the file is 149,499 on the server, it finishes its download at 145,996. Why? How do I get the last 2% or so to flush and complete the download? Thank much for your help.

    FYI, this also happens the same on smaller files so its not stopping for time or file size issues.

     
    $path = "the/file/path.mp4";
    
    $headsize = get_headers($path,1);
    
    $ext = str_from_last_occurrence($_vars['filepath'],".");
    
    if ($ext=="mp3") { $type = "audio"; }
    elseif ($ext=="mp4") { $type = "video"; }
    
    function readfile_chunked($filename,$retbytes=true) {
    
    	// Stream file
    	$handle = fopen($filename, 'rb');
    	$chunksize = 1*(1024*1024); // how many bytes per chunk
    	$buffer = '';
    	$cnt =0;
    
       if ($handle === false) {
           return false;
       }
    
       while (!feof($handle)) {
           $buffer = fread($handle, $chunksize);
           echo $buffer;
           ob_flush();
           flush();
           if ($retbytes) {
               $cnt += strlen($buffer);
           }
       }
    
       $status = fclose($handle);
    
       if ($retbytes && $status) {
           return $cnt; // return num. bytes delivered like readfile() does.
       }
    
       return $status;
    
    }
    
    header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
    header("Content-type: ".$type."/".$ext);
    header('Content-Length: ' . (string)($headsize['Content-Length']));
    header('Content-Disposition: attachment; filename="'.str_from_last_occurrence($_vars['filepath'],"/").'"');
    header("Content-Transfer-Encoding: binary");
    
    readfile_chunked($path);
    
    exit;
    
    Code (markup):
     
    cesarcesar, Feb 9, 2011 IP
  2. moads

    moads Member

    Messages:
    115
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    28
    #2
    Check your php.ini file to be sure that PHP isn't timing out. I think the variable is "set_time_limit"
     
    moads, Feb 9, 2011 IP
  3. cesarcesar

    cesarcesar Peon

    Messages:
    188
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    it is not timing out. like i said in my post, i get the same effect on smaller files. yes i verified my php.ini as well.
     
    cesarcesar, Feb 10, 2011 IP
  4. mallorcahp

    mallorcahp Peon

    Messages:
    141
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Just a thought ... have you tried without the "exit;" statement ??
     
    mallorcahp, Feb 10, 2011 IP
  5. hogan_h

    hogan_h Peon

    Messages:
    199
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Script is working fine for me, local web server, wamp 2.1, php 5.2

    Check the value of $headsize, if it's equivalent to the real file size, that's the first thing coming to my mind right now.
     
    hogan_h, Feb 10, 2011 IP