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
Release: 2024-12-13 11:24:43
Original
497 people have browsed it

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);
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template