Simple question, probably been asked a million times but I've searched and am no closer to getting this to work.
I have a PHP script within the UserFrosting framework that serves a file to the user via their browser. I would like the file to open in their browser if they select 'Open' insteand of download.
Problem is, the only things that are opening properly are text files. Anything else (PDFs and JPGs tested so far) are just opening as a page of ASCII/garbled code instead of being interpreted as a PDF or an image.
Here's the code I'm using:
header('Content-Type: '. $mime);
header('Content-Length: ' . filesize($diskname));
header('Content-Disposition: inline; filename=' . $savename);
return readfile($diskname);
$mime
is the Mime type of the file set via a function, so could be application/pdf
or image/jpeg
for the files I'm trying to view so far.
Does anyone have any ideas why the files aren't opening properly?
UPDATE
Having checked the response headers, I'm getting a Content Type of text/html;charset=UTF-8
instead of the application/jpeg
or application/pdf
that it should have been set to according to the code I'm using. I'm wondering if this is a UserFrosting issue?
Okay, problem solved. As I suspected, it was to do with the UserFrosting framework, or more specifically, Slim.
Instead of settings the headers using header('Content-Type: image/jpeg');
, I should have been doing the following:
$this->_app->response->headers->set('Content-Type', $mime);
Problem solved. :)