Content type in Header

Hello,

I’m trying to use the following code (download.php)

[code]<?php
//header(‘Content-type: application/pdf’);
if(isset($_GET[‘id’]))
{
include ‘config.php’;
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-length: $size”);
header(“Content-type: $type”);
header(“Content-Disposition: attachment; filename=$name”);
echo $content;

include ‘closedb.php’;
exit;
}

?>[/code]

to download a file from a mysql database. When I specify the id by putting download.php?id=1, what should be a pdf comes out as

%PDF-1.6 %???? 4 0 obj <> endobj xref 4 104 0000000016 00000 n 0000002710 00000 n 0000012979 00000 n 0000013364 00000 n 0000013505 00000 n 0000013773 00000 n 0000013909 00000 n 0000014057 00000 n 0000014201 00000 n 0000014348 00000 n 0000014490 00000 n 0000014637 00000 n 0000014780 00000 n 0000014982 00000 n 0000015334 00000 n 0000015564 00000 n 0000015641 00000 n 0000015999 00000 n 0000016235 00000 n 0000016443 00000 n 0000016796 00000 n 0000017027 00000 n 0000017386 00000 n 0000017623 00000 n 0000017831 00000 n 0000018184 00000 n 0000018415 00000 n 0000018774 00000 n 0000019011 00000 n 0000019219 00000 n 0000019572 00000 n 0000019803 00000 n 0000020162 00000 n 0000020399 00000 n 0000020607 00000 n 0000020960 00000 n 0000021191 00000 n 0000021550 00000 n 0000021787 00000 n 0000021995 00000 n 0000022348 00000 n 0000022579 00000 n 0000022938 00000 n 0000023175 00000 n 0000023382 00000 n 0000023735 00000 n 0000023966 00000 n 0000024325 00000 n 0000024562 00000 n 0000024769 00000 n 0000025122 00000 n 0000025353 00000 n 0000025712 00000 n 0000025949 00000 n 0000026156 00000 n 0000026509 00000 n 0000026740 00000 n 0000027099 00000 n 0000027336 00000 n 0000027544 00000 n 0000027897 00000 n 0000028128 00000 n 0000028487 00000 n 0000028724 00000 n 0000028863 00000 n 0000029004 00000 n 0000029148 00000 n 0000029287 00000 n 0000029425 00000 n 0000029564 00000 n 0000029707 00000 n 0000029851 00000 n 0000029994 00000 n 0000030138 00000 n 0000030283 00000 n 0000030427 00000 n 0000030566 00000 n 0000030706 00000 n 0000030846 00000 n 0000030985 00000 n 0000031123 00000 n 0000031330 00000 n 0000031683 00000 n 0000031914 00000 n 0000032273 00000 n 0000032510 00000 n 0000032681 00000 n 0000032983 00000 n 0000033073 00000 n 0000034265 00000 n 0000034374 00000 n 0000034451 00000 n 0000037650 00000 n 0000040946 00000 n 0000044211 00000 n 0000047422 00000 n 0000050865 00000 n 0000054175 00000 n 0000057566 00000 n 0000060360 00000 n 0000060450 00000 n 0000060545 00000 n 0000060611 00000 n 0000002376 00000 n trailer <<210C6359FB573646A30EFA6359E23F2E>]>> startxref 0 %%EOF 107 0 obj <>stream x?b``He`0b†]? ???Y8?0`???IM*3082??0??lpg5?gU?eU???(??#°???W???~!†eB kpp? ?‡??¬?–?, ?X>Q?a/– ?@??,? ’n: g?N?i8?????«&H2>`d B??†d,la????„D‡N1™J???3?2»??XZl.???|§^u??{2®n.}#±)dm?••“W???¤?9??U?8?o??A?a y? ?…?4??&?‹?e? endstream endobj 5 0 obj </M

etc, etc.

I know the problem is that the browser is not getting the content-type via this line

header("Content-type: $type");

because if I uncomment this line

//header('Content-type: application/pdf');

everything works fine.

Any help would be much appreciated.

-Robert

try
header("Content-type: echo $type; ");
instead of
header(“Content-type: $type”);

Sorry if last one does not work … this is better… try this :

<?php echo "header("Content-type: $type ")"; ?>

Hi amitbravo,

Thanks for the reply and your help.

I tried both codes above and neither worked. The second one, however, did offer a hint to what might be happening. When using this code, this is what is displayed in the browser when the file type is PDF:

header("Content-type: ") %PDF-1.6 %???? 1 0 obj <> endobj 2 0 obj <> endobj 4 0 obj <> endobj 5 0 obj <> endobj 6 0 obj <<>/ProcSet[/PDF/Text]/ExtGState<>>> endobj 8 0 obj <>stream 

This part of the code above

header("Content-type: ")

makes me think that the browser still isn’t receiving $type from the server. This error occurs in every browser I’ve tested (firefox, IE, opera, mozilla, safari and flock).

Thanks
-Robert

AFAIK, you can only set a certain header type once. You’re trying to set header('Content-type: ') twice. This is probably the reason your script fails when you uncomment the first header() call.

Hi Zyppora,

Thanks for advice.

Even with only 1 header(“content-type”), the script still didn’t send the file type to the browser. But for some reason (which I won’t pretend to understand), adding

ob_start();

as the first line of code made it work.

So the final code is

[code]<?php
ob_start();

if(isset($_GET[‘id’]))
{
include ‘config.php’;
include ‘opendb.php’;

$id = $_GET[‘id’];
$query = "SELECT name, type, size, content " .
“FROM documents WHERE id = ‘$id’”;

$result = mysql_query($query) or die(‘Error, query failed’);
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;

include ‘closedb.php’;
exit;
}

?>[/code]

ob_start() starts output buffering, meaning anything that’s being sent to the client is being held by the output buffer, and only sent collectively when the script ends (or when some other function was called, I believe it was ob_end_flush(), but not sure). I’m not sure why PHP shows such behaviour, perhaps the processing is slow enough to let the engine (or even the server software) time out, automatically flushing whatever’s been sent into the browser as plaintext?

Makes sense.

The code above only seems to work for the PDF file format. Word docs, for example, are still messed up. I guess in a way this is good because it will force me to convert all my docs to PDFs.

Sponsor our Newsletter | Privacy Policy | Terms of Service