MKD MKD - 1 month ago 13x
PHP Question

Content-Type: image/jpeg not working on live working on local

I am very new to slim framework and i am trying to download a image file. Working fine on local with core php . But when applying same code on server for slim its not working . Searched a lot But no luck i.e here and this one. here is my code

$app->get('/image', function ($request, $response, $args) {
//header('Content-type: image/png');
//header('Content-Disposition: attachment; filename=go-logo.jpg');
//$contents = file_get_contents(getcwd() . '/' . $_GET['img']);
$filename=getcwd() . '/' . $_GET['img'];
header("Content-Length: " . filesize($filename));
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=go-logo.jpg');
return $contents;


enter image description here

Error is it is showing it is a html file . i want like this

enter image description here


You need to set the headers on the response object. Then write the file to the Response body.

return $response->withHeader("Content-Type", "application/octet-stream")