Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP image show on browser

Tags:

php

(1)

$file = $_GET['file'];
echo '<img src="'.$file.'" />';

(2)

 $file = $_GET['file'];
 $imginfo = getimagesize($file);
 header('Content-type: '.$imginfo['mime']);
 echo file_get_contents($file);`

From these 2 code, my image can show in the browser nicely. But what is the differences of them? Which method should I prefer?

like image 517
Michael Kuan Avatar asked Nov 27 '25 17:11

Michael Kuan


1 Answers

The first example you've posted is simply "including" the image file into the DOM. It would essentially output something like:

<img src="path/to/image.png" />

While the second option actually sets the Content-Type to whatever the mime of the image is. Meaning if it's a png for example, the page that runs that script will actually be served as a whole image.

If it was a png image, it'd return the content type of image/png.

like image 83
Darren Avatar answered Nov 29 '25 12:11

Darren



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!