(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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With