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

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 »

Try switching out the content-type:

application/x-msexcel
application/ms-excel
application/msexcel
application/x-excel

Try commenting it out too.
gerryjuice
Forum Newbie
Posts: 12
Joined: Fri Jan 19, 2007 8:37 am

Post by gerryjuice »

commenting out

Code: Select all

header("Content-type: $type");
has no effect on the current issue.
As far as i know type of the file is picked up by

Code: Select all

$fileType = $_FILES['userfile']['type'];
Changing the application type as suggested before calling

Code: Select all

echo $content
makes no difference.

Is there a simpler way to do this, or am i missing something.
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 »

And you tried quotes around $name in Content-Disposition
gerryjuice
Forum Newbie
Posts: 12
Joined: Fri Jan 19, 2007 8:37 am

Post by gerryjuice »

Put "" around the $name variable in

Code: Select all

header("Content-Disposition: attachment; filename=$name");
Which resulted in nothing coming out on the page at all. Does this mean an error occured.

Here is my code currently:

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
	<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
	<title>Download File From MySQL</title> 
	</head> 
	
	<body> 
	<?
	if(isset($_GET['id'])) 
	{ 
		include("db.inc.php"); 
			mysql_connect($databaseServer,$user,$passwd) or
		  die("Could not connect to the database.<br>".mysql_error());
			  mysql_select_db($database) or 
		  die("Could not select your database.<br>".mysql_error());
	
		$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); 
		echo $name;
		echo $type;
	
		
		header('Pragma: private');
		header('Cache-control: private, must-revalidate');
		header('Content-Transfer-Encoding: Binary'); 
		header('Content-Type: application/octet-stream; filename=$name');
		header("Content-Disposition: attachment; filename=$name"); 
		//header("Content-length: $size"); 
		//header("Content-type: $type"); 
		echo $content; 
		//echo ("Content-Disposition: attachment; filename=$name"); 
		//echo ("Content-length: $size"); 
		//echo ("Content-type: $type"); 
		mysql_close();  
		exit; 
	} 
	 
		include("db.inc.php");
		mysql_connect($databaseServer,$user,$passwd) or die("Could not connect to the database.<br>".mysql_error());
		mysql_select_db($database) or die("Could not select your database.<br>".mysql_error());
	
		$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> 
	<?         
		} 
	} 
		mysql_close();
	?> 
	</body> 
	</html>
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 »

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
        <title>Download File From MySQL</title>
        </head>
       
        <body> 
The headers probably aren't being sent because this is being outputted to the browser. Only display this if the user isn't download the file.

Code: Select all

header('Content-Type: application/octet-stream; filename=$name');
That needs to be in double-quotes, and I'm not even sure that the filename belongs in the content-type header.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

I was just going to mention that, I feel retarded because I hadn't pointed that out earlier.

Please make sure that PHP is reporting all errors.
Post Reply