Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Downloading files saved in database.

Status
Not open for further replies.

jsteiner87

Programmer
Oct 9, 2003
76
US
I am having problems with my download script and IE. If I use the download script above my browser asks me if I want to save it or open it. When I save it to my desktop it opens up with no errors. If I try to open it without saving it to the desktop, it gives me this error in Adobe. "There was an error opening this document. The file does not exist." When I try this in FireFox I don't get an error either way.

This is my download script in my header.

<?php
if(isset($_GET['id']))
{
$id = $_GET['id'];
mysql_select_db($database_conn, $database);
$query = "SELECT file_name, file_type, file_size, file_content FROM table WHERE id = '$id'";
$result = mysql_query($query, $database) or die(mysql_error());
list($name, $type, $size, $content) = mysql_fetch_array($result);

header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
echo $content;
exit;
}
?>
 
I've read somewhere that you always have to pass Content-type first. You might also want include header('Content-Transfer-Encoding: binary');

Some browsers handle Content-Disposition differently so you might want to have some browser detection code:

Code:
$browser= $_SERVER['HTTP_USER_AGENT'];
if (strstr('MSIE 5.5', $browser) || strstr('MSIE 6.01', $browser))
{
	header('Content-Disposition: filename="'.$fileName.'"');
}
else
{
	header('Content-Disposition: attachment; filename="'.$fileName.'"');
}


--== Anything can go wrong. It's just a matter of how far wrong it will go till people think its right. ==--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top