Page 1 of 1

PHP force file download

Posted: Wed Feb 16, 2011 1:27 pm
by TonsOfFun
I have script that forces a file to download when you click a link.
The problem is that when you download it, the file is messed up.

Here's the code:

Code: Select all

$file_id = $_GET['file_id'];
$query = "SELECT * FROM table WHERE f_id = ".$file_id;
$result = @mysql_query($query);
	while($row = mysql_fetch_array($result)) {
		$type = $row['f_type'];
		$file = $row['f_file'];
		header('Content-type: ' . $type . '');
		header('Content-Disposition: attachment; filename="' . $file . '"');
		header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
		header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
	}
$row['f_type'] is the MIME file type, i.e. application/msword

Thanks for any help.

Re: PHP force file download

Posted: Wed Feb 16, 2011 1:38 pm
by John Cartwright
You are never actually outputting the file. See readfile()