Welcome to a tutorial on how to display an image from a file in PHP. So you have a secured folder of images that is not publicly accessible, but still want registered users to be able to view? Yes, it is possible to do so.
To display an image from a file in PHP, simply output the necessary HTTP headers and read the image file:
header("Content-Type: image/jpeg");
header("Content-Length: " . filesize("IMAGE.JPG"));
readfile("IMAGE.JPG");
That should cover the basics, but read on for a few more examples!
TLDR – QUICK SLIDES
Fullscreen Mode – Click Here
TABLE OF CONTENTS
PHP DISPLAY IMAGE
All right, let us now get into the examples of displaying an image file in PHP.
EXAMPLE 1) READ FILE AND OUTPUT
<?php
// (A) GET IMAGE INFO
$file = __DIR__ . DIRECTORY_SEPARATOR . "cabbage-girl.jpg";
$fileData = exif_read_data($file);
// (B) READ & OUTPUT IMAGE
ob_start();
header("Content-Type: " . $fileData["MimeType"]);
header("Content-Length: " . $fileData["FileSize"]);
readfile($file);
ob_end_flush();
Yep, that’s how simple it is.
- (A) Read the EXIF information from the image file.
- (B) Output the appropriate HTTP headers, the bare minimum is
Content-Type
, andContent-Length
is kind of optional. But since some browsers can get naggy about the length, it is better to just include it. - (B) Lastly, just use
readfile()
to read and output the image file itself. - (B) To ensure that the image data is “safely and completely” delivered, we use output buffering –
ob_start()
andob_end_flush()
. Will leave links below if you want to follow up on output buffers.
That’s all. Accessing this script in the browser will show the image, or we can point an image tag to this PHP script:
<img src="1a-output-img.php">
EXAMPLE 2) BASE 64 ENCODED IMAGE
<?php
// (A) READ IMAGE INFO
$file = __DIR__ . DIRECTORY_SEPARATOR . "cabbage-girl.jpg";
$fileData = exif_read_data($file);
$fileEncode = base64_encode(file_get_contents($file));
// (B) BASE 64 ENCODED IMAGE
echo "<img src=\"data:${fileData["MimeType"]};base64,${fileEncode}\">";
?>
This is an alternative to reading an image file and outputting it with PHP – Yes, we can read an image file, base 64 encode it, and “directly embed” it into an <img>
tag.
DOWNLOAD & NOTES
Here is the download link to the example code, so you don’t have to copy-paste everything.
SUPPORT
600+ free tutorials & projects on Code Boxx and still growing. I insist on not turning Code Boxx into a "paid scripts and courses" business, so every little bit of support helps.
Buy Me A Meal Code Boxx eBooks
EXAMPLE CODE DOWNLOAD
Click here for the source code on GitHub gist, just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.
EXTRA BITS & LINKS
That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.
LINKS & REFERENCES
- Image headers with PHP – The Electric Toolbox Blog
- Base64 Encode – PHP
- Output Buffering – Code Boxx
INFOGRAPHIC CHEAT SHEET

THE END
Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!
I can’t get any of this to run. What do you run first 1a or 1b or ??? I think 1b ends up showing an empty square box in the upper left corner of the screen but clicking on it does nothing. Do you run it with localhost ? firefox ? php ?
Thanks in advance for help in getting this to run.
R
Put this tutorial aside first. Don’t break your own neck by skipping all the raw basics – Good luck!
https://www.apachefriends.org/
https://code-boxx.com/how-to-run-php-scripts/
https://www.tutorialspoint.com/php/index.htm