简体   繁体   中英

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

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?

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">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM