Home > Blockchain >  Displaying image with Content-Disposition: inline from database using PHP
Displaying image with Content-Disposition: inline from database using PHP

Time:10-17

I wish to embed my images' binary content within my database and then return them via a PHP script.

I have successfully stored the data in the database and I can query the database. I am able to create a file download page.

header('Content-Type: '.$img_filetype);
header('Content-Disposition: attachment; filename="'.$img_filename.'"');
echo($img_binary_content);

Example, on my website: https://davidsmedberg.me/img/test-sqlite.php?id=1

However, what I want is to display the image inline so that it can be included as a src for an img tag in an HTML document elsewhere. Just like I can with this file: https://www.davidsmedberg.me/img/thames.jpg

When I try replacing the Content-Disposition header like so:

header('Content-Type: '.$img_filetype);
header('Content-Disposition: inline');
echo($img_binary_content);

I get a malformed image. Firefox, for example, says The image cannot be displayed because it contains errors. Example, on my website: https://davidsmedberg.me/img/test-sqlite1.php?id=1

The same thing happens if I don't explicitly set the Content-Disposition header.

Can anyone help me figure out what I'm doing wrong here?

CodePudding user response:

Your should remove the following html code from your php script, these extra HTML tags appear at the head of image content, cause browser not able to recognize the image

<!DOCTYPE html>
<html lang="en">
  • Related