We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

display file in the browser

how I can display a file in the browser instead of being downloaded?

I read about the content-disposition header with inline type, but it doesn't works!

$response = new \Phalcon\Http\Response();
$response->setHeader('Content-Disposition','inline; filename=' . $filename);
$response->sendHeaders();

i'm doing some wrong, or exists another solution



36.8k

I think I found a solution. I don't know if it is correct, but it has worked. Only I added the content-type header with mime of the file as parameter

$response = new \Phalcon\Http\Response();
$response->setHeader('Content-Description','Display File');
$response->setHeader('Content-Type',$mimeType);
$response->setHeader('Content-Disposition','inline; filename=' . $filename);
$response->sendHeaders();

Smth like this:

$response = new \Phalcon\Http\Response();
$response->setHeader('Content-Disposition', 'inline; filename=' . urlencode($flename));
$response->setContentType('image/jpeg');
$response->setFileToSend('/path/to/file', null, false);
$response->send();