Home >Backend Development >PHP Tutorial >How Can I Display an Image in a Browser Using PHP?

How Can I Display an Image in a Browser Using PHP?

Susan Sarandon
Susan SarandonOriginal
2024-12-13 11:24:43498browse

How Can I Display an Image in a Browser Using PHP?

Displaying Images in PHP

This question focuses on displaying an image in the browser using PHP. The problem involves accessing an image file, retrieving its MIME type, and then sending it as a response to the browser.

Solution:

The solution provided involves using the header() function to set the appropriate MIME type for the image, and the filesize() function to determine the size of the image file. The readfile() function is then used to read the contents of the image file and output it to the browser.

Here's a code snippet that demonstrates the solution:

$file = '../image.jpg';
$type = 'image/jpeg';
header('Content-Type:'.$type);
header('Content-Length: ' . filesize($file));
readfile($file);

By following these steps, the image file will be successfully displayed in the browser.

The above is the detailed content of How Can I Display an Image in a Browser Using PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn