Home > database >  Header content type: image/jpeg not working
Header content type: image/jpeg not working

Time:12-27

I am new to php and trying to display the image uploaded to form, the code is working as long as I don't specify the header content type. But I want to specify the content type to image/jpeg, the image which was otherwise being displayed is not displayed anymore when content type is applied.It just shows a small box on the top of page with a blue question mark inside and a blue background. This code is working:

if ($filetype == "text/plain"){
    header('Content-type:text/plain');
    echo file_get_contents("./images/$filename");
    } 
else if ($filetype == "image/jpeg"){
    //header('Content-type: image/jpeg');
    echo "<img src='images/$filename'>";
    }

but its not working with header('Content-type: image/jpeg')

I tried it with echo get_file_contents function as well, but it results in binary code for image without header type, and the same blue question mark thing with header type.

CodePudding user response:

//header('Content-type: image/jpeg');
echo "<img src='images/$filename'>";

echo "<img src='images/$filename'>"; is going to output a string of HTML. That isn't a JPEG. It has a URL to a JPEG inside it.

The correct content type is text/html.

  • Related