header("Content-type: $type") Ignored in Internet

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

gerryjuice
Forum Newbie
Posts: 12
Joined: Fri Jan 19, 2007 8:37 am

header("Content-type: $type") Ignored in Internet

Post by gerryjuice »

feyd | Please use

Code: Select all

,

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

,

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]
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post 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?
gerryjuice
Forum Newbie
Posts: 12
Joined: Fri Jan 19, 2007 8:37 am

Post 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
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post 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"');
gerryjuice
Forum Newbie
Posts: 12
Joined: Fri Jan 19, 2007 8:37 am

Post 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.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

gerryjuice wrote:

Code: Select all

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
try removing that.
gerryjuice
Forum Newbie
Posts: 12
Joined: Fri Jan 19, 2007 8:37 am

Post by gerryjuice »

Ya took that out.
Same problem. :cry: :cry: :cry:
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post 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
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post 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");
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post 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)
gerryjuice
Forum Newbie
Posts: 12
Joined: Fri Jan 19, 2007 8:37 am

Post by gerryjuice »

Hi ive taken out the line

Code: Select all

echo $content
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
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

gerryjuice wrote:Content-type: appliaction/vnd.ms-excel
The misspelling could be part of your problem
gerryjuice
Forum Newbie
Posts: 12
Joined: Fri Jan 19, 2007 8:37 am

Post by gerryjuice »

Sorry mis-type rather than mis-spelling. Unfortunately :oops:
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Have you tried removing the content-length header like I suggested?
gerryjuice
Forum Newbie
Posts: 12
Joined: Fri Jan 19, 2007 8:37 am

Post by gerryjuice »

Yes i commented it out. Still seems to be behaving the same way.
Post Reply