Monsters X
Monsters X

Reputation: 2646

PHP header isn't modifying the content/type

I can't get php to set various headers when trying to return a file.

Relevant code:

if (ob_get_contents()) ob_end_clean();
header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
header('Content-Type', 'application/pdf');
if (ob_get_contents()) ob_flush();
header('Content-Disposition', 'attachment; filename="' . basename($setfile) . '"');
header('Content-length', filesize($fullpath));

readfile($fullpath);

File exists, all is well. but the header response is always:

HTTP/1.1 200 OK
Date: Sat, 09 Nov 2013 22:19:22 GMT
Server: Apache/2.2.24 (Unix) DAV/2 PHP/5.5.0 mod_ssl/2.2.24 OpenSSL/0.9.8y
X-Powered-By: PHP/5.5.0
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 20
Content-Type: text/html

I can modify the status code, i've returned 404 etc.. but the content-type is always text/html... I'm at a bit of a loss.. The content-disposition isnt being set.. hmm...

Serving the file requires a valid session, so please no 'just redirect to the file' type answers.

Thanks for any input

Upvotes: 0

Views: 754

Answers (2)

Ravish
Ravish

Reputation: 2421

what about this :

<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');

// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="YOUR_FILE.pdf"');

@rob is right, you are calling header incorrectly. also, see if you are actually able to read the file.

Upvotes: 1

Rob
Rob

Reputation: 12872

You are calling header() incorrectly. You want

header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="' . basename($setfile) . '"');

See http://php.net/manual/en/function.header.php

Upvotes: 2

Related Questions