views:

268

answers:

2

I created a file download script in PHP, it works, but web browsers report the file as "Unknown Length". My code is as follows:

function downloadFile($file){
  // Set up the download system...
  header('Content-Description: File Transfer');
  header('Content-Type: '.mime_content_type($file));
  header('Content-Disposition: attachment; filename="'.basename($file).'"');
  header('Content-Transfer-Encoding: binary');
  header('Expires: 0');
  header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  header('Pragma: public');
  header('Content-Length: '.filesize($file));

  // Flush the cache
  ob_clean();
  flush();

  // Send file to browser
  readfile($file);

  // DO NOT DO ANYTHING AFTER FILE DOWNLOAD
  exit;
}
A: 

Try not flushing the cache before readfile() function. My code is almost identical to yours otherwise, and works fine.

Yegor
Tried that, nothing. The download still works fine, it's just that web browsers will say "unknown file size" even though I gave it a "Content-length". Thing is, if I use wget, it reports the correct file size.
Rocket
+1  A: 

http://paul.luminos.nl/update/471

Honso
Interesting. I'll need to look into that.
Rocket