Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell if a URL points to an image?

Tags:

php

I have a php page with a text input where the user is supposed to paste a remote URL of an image, and I will have to store it in the server and display it to the user. Now the problem is, I don't trust a user will always provide a proper image url, and I don't want them to upload a pdf or other file, or a huge, few gb worth of file. Now I can check the extension, but that isn't very helpful, and I hear I can check the mime-type, but I don't know how I can open the file once and check all the validations like mime-type and file size in one go, and then copy the file over. Moreover, since the file will be pretty much served as it is(with a minor name change), I would like to know if it is possible to make sure that the file doesn't have any injected virus or problematic code.

Any suggestions appreciated.

like image 498
Bluemagica Avatar asked Nov 28 '25 04:11

Bluemagica


1 Answers

You can use exif_imagetype() to see if its an image.

If you want to be 100% sure that its not malware or something weird. its a good idea to use the GD library and save it via the GD library. So there is no dangerous code inside.

like image 183
S.Visser Avatar answered Nov 30 '25 16:11

S.Visser



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!