[ciapug] fpassthru file download

Barry Von Ahsen barry at vonahsen.com
Wed Dec 5 15:33:14 CST 2007


so I had to revisit this, and it seems a lot of people have this 
problem, but haven't found a solution.

I want users to download a file that preferably lives outside of the 
webroot.  it works fine in FF and IE7, but IE6 won't pick up the file 
name I specify, it wants to name it my page name (download.php) - I 
accept that IE6 SP2 will force a download, I just want the name to be 
right.  anybody fixed this?


here's my code:

$ext = pathinfo($thefile,PATHINFO_EXTENSION);
$file = $download_dir.$thefile;
if($fh = fopen($file,"rb")) {
	/*if(function_exists(mime_content_type)) {
		header("Content-Type: ".mime_content_type($file));
	} else {*/
		switch($ext) {
			case "pdf": header("Content-Type: application/pdf"); break;
			case "doc": header("Content-Type: application/msword"); break;
			case "csv": header("Content-Type: application/msexcel"); break;
			case "exe": header("Content-Type: application/force-download"); break;
			case "mp3": header("Content-Type: audio/mpeg"); break;
			case "txt": header("Content-Type: text/plain"); break;
			case "zip": header("Content-Type: application/zip"); break;
			default: header("Content-Type: application/force-download"); break;
		}
	//}
	header("Content-Disposition: attachment; filename=\"$thefile\"");
	header("Content-Length: " . filesize($file));
	header('Cache-Control: public');
	header('Pragma: public');
	fpassthru($fh);
	exit();
}


More information about the ciapug mailing list