Page 1 of 2
header("Content-type: $type") Ignored in Internet
Posted: Fri Jan 19, 2007 8:51 am
by gerryjuice
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Im trying a content management sys using PHP. Im uploading files to a database based around the following fields fileID, userID, filename, filesize, filetype. I seem to be having problems downloading the files in internet explorer they are being printed out as gibberish on the page, rather than using the correct appliaction.
Code: Select all
$sql = "SELECT bin_data, filetype, filename, filesize FROM tbl_Files WHERE id_files=$id_files";
$result = @mysql_query($sql, $db);
$data = @mysql_result($result, 0, "bin_data");
$name = @mysql_result($result, 0, "filename");
$size = @mysql_result($result, 0, "filesize");
$type = @mysql_result($result, 0, "filetype");
header("Content-type: $type");
header("Content-length: $size");
header("Content-Disposition: attachment; filename=$name");
header("Content-Description: PHP Generated Data");
echo $data;
Internet eplorer seems to ignore header("Content-type: $type");. Is there a way to store the file extension when uploading. And then use that file extension opening a file
Or is their a better work around
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Posted: Fri Jan 19, 2007 12:39 pm
by daedalus__
This is a big no-no.
Code: Select all
$result = @mysql_query($sql, $db);
$data = @mysql_result($result, 0, "bin_data");
$name = @mysql_result($result, 0, "filename");
$size = @mysql_result($result, 0, "filesize");
$type = @mysql_result($result, 0, "filetype");
Are you storing the proper mime-type?
Posted: Fri Jan 19, 2007 12:46 pm
by gerryjuice
Have looked into database at how the information is being stored and the fileType field has the correct information associated with it.
i.e. If i upload a word document the fileType = application/msword
Posted: Fri Jan 19, 2007 12:50 pm
by daedalus__
You need to remove those error suppression operators and do proper error-checking. Wrapping those calls inside if statements may shed some light on this problem, are you sure the correct data is being pulled?
What version of internet explorer are you using?
I don't know what this does but I saw it in the manual, perhaps you should try it:
Code: Select all
// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');
Posted: Fri Jan 19, 2007 1:57 pm
by gerryjuice
Right ive changed my code like so
Code: Select all
<?
if(isset($_GET['id']))
{
include 'opendb.php';
$id = $_GET['id'];
$query = "SELECT name, type, size, content FROM upload WHERE id = '$id'";
$result = mysql_query($query) or die('Error, query failed');
list($name, $type, $size, $content) = mysql_fetch_array($result);
header("Content-Disposition: attachment; filename=$name");
header("Content-length: $size");
header("Content-type: $type");
echo $content;
include 'closedb.php';
exit;
}
?>
<html>
<head>
<title>Download File From MySQL</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?
include 'opendb.php';
$query = "SELECT id, name FROM upload";
$result = mysql_query($query) or die('Error, query failed');
if(mysql_num_rows($result) == 0)
{
echo "Database is empty <br>";
}
else
{
while(list($id, $name) = mysql_fetch_array($result))
{
?>
<a href="download.php?id=<?=$id;?>"><?=$name;?></a> <br>
<?
}
}
include 'closedb.php';
?>
</body>
</html>
Still getting the same result, that the content is outputting to an internet explorer page, instead of opening in the related application.
Posted: Fri Jan 19, 2007 2:16 pm
by daedalus__
gerryjuice wrote:Code: Select all
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
try removing that.
Posted: Fri Jan 19, 2007 2:28 pm
by gerryjuice
Ya took that out.
Same problem.

Posted: Fri Jan 19, 2007 4:04 pm
by daedalus__
gerryjuice wrote:
Code: Select all
<?
if(isset($_GET['id']))
{
include 'opendb.php';
$id = $_GET['id'];
$query = "SELECT name, type, size, content FROM upload WHERE id = '$id'";
$result = mysql_query($query) or die('Error, query failed');
list($name, $type, $size, $content) = mysql_fetch_array($result);
header("Content-Disposition: attachment; filename=$name");
header("Content-length: $size");
header("Content-type: $type");
echo $content;
include 'closedb.php';
}
else
{
// stuff
}
im out of ideas but perhaps you aren't storing the data right
Posted: Fri Jan 19, 2007 4:08 pm
by nickvd
Try echo'ing the header statements to ensure you're sending the correct stuff..
Code: Select all
echo("Content-Disposition: attachment; filename=$name");
echo("Content-length: $size");
echo("Content-type: $type");
Posted: Fri Jan 19, 2007 4:33 pm
by aaronhall
Always make sure you use mysql_real_escape_string() on $_GET/$_POST/$_REQUEST values before sticking them into queries -- it wouldn't be hard for someone delete everything in your database the way it is right now.
First, I would try dumping the Content-length header, and seeing if it works. Quotes around $name in the Content-Disposition header wouldn't hurt. Is the file extension already appended to $name?
(And do what nickvd said)
Posted: Fri Jan 19, 2007 5:00 pm
by gerryjuice
Hi ive taken out the line
And put in the following lines
Code: Select all
echo("Content-Disposition: attachment; filename=$name");
echo("Content-length: $size");
echo("Content-type: $type");
This is my output
Content-Disposition: attachement; filename=bank.xls
Content-length: 158872
Content-type: appliaction/vnd.ms-excel
Posted: Fri Jan 19, 2007 5:02 pm
by aaronhall
gerryjuice wrote:Content-type: appliaction/vnd.ms-excel
The misspelling could be part of your problem
Posted: Fri Jan 19, 2007 5:10 pm
by gerryjuice
Sorry mis-type rather than mis-spelling. Unfortunately

Posted: Fri Jan 19, 2007 5:13 pm
by aaronhall
Have you tried removing the content-length header like I suggested?
Posted: Fri Jan 19, 2007 5:26 pm
by gerryjuice
Yes i commented it out. Still seems to be behaving the same way.