peter_s_ratchet_app

Peter's Ratchet App

PHP has a built-in function named http_send_file that sends a file to the browser and returns true for a success and false for a failure.

You could create a function that sends the file in chunks and keeps track of whether the whole file was sent.

function readfile_chunked ($filename) { 
  $chunksize = 1*(1024*1024); // how many bytes per chunk 
  $buffer = ''; 
  $handle = fopen($filename, 'rb'); 
  if ($handle === false) { 
    return false; 
  } 
  while (!feof($handle)) { 
    $buffer = fread($handle, $chunksize); 
    print $buffer;
    ob_flush();
    flush();
  }
  
// You could either count the number of chunks sent or maybe test for eof to determine if the entire file was downloaded.
  if (feof($handle))
    // Flag the file as downloaded in the db. 
    
  return fclose($handle); 
}
  • peter_s_ratchet_app.txt
  • Last modified: 2013/07/27 21:08
  • by mmuze