PHP force file download

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
TonsOfFun
Forum Commoner
Posts: 54
Joined: Wed Jun 02, 2010 7:37 pm

PHP force file download

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: PHP force file download

Post by John Cartwright »

You are never actually outputting the file. See readfile()
Post Reply