Jump to content

check to see if image exists at URL?


galvin

Recommended Posts

What is the best/proper way to check if a supplied URL goes to an actual image?  My site will allow users to enter an image URL (like "http://example.com/images/monkey.jpg").  I will store the URL they enter in a MySQL database and then will call it up later and do some resizing to these images. 

 

But I don't want to try to do any resizing if the URL they supply doesn't actually go to an image, or if it's blank (i.e. the user didn't enter a URL yet). 

 

So basically, I will run a MySql query to pull back the contents of the column called "imageurl".  Let's say those contents are stored in a variable called $imageurl.  This is what I'm trying to accomplish....

 

if ($theimageurl does NOT contain a URL to an image OR is blank) {
// do this
} else {
//run my resizing code
}

 

I guess i could do this to check to see if it's blank, but not sure this is proper/most efficient way to check that part of the issue...

 

if (trim($theimageurl) == "")

 

But greater than the "is it blank" concern is that people may enter URLs that just to a regular website so whether it's at this point in my code (or for security, I should probably do it BEFORE inserting it into my database :/ ) so curious if there are any functions that check to see if it's an image URL or not.  I know of file_exists, but I'm pretty sure that's just checking to see if a file exists and doesn't check image URLs.

 

Any info/suggestions would be appreciated as always! 

 

And if there are any recommended functions to check/clean the user-entered URL BEFORE it's entered into the database, please share :)

 

Thanks!

Link to comment
Share on other sites

Ok thanks!  As I think more about maybe "cleaning" the URLs that users enter before inserting them into my database, I don't think I need to because I only re-display those URLs inside an <img> tag, in the "src".  So even if someone enters the URL of an actual website, it will just show a broken image, which I'm fine with (for now).  There's no way a bad link could hurt my site if it's only displayed back inside the <img> tag, right? 

 

Or am I being really, really naive?  :)

 

 

Link to comment
Share on other sites

That's a subjective question. Someone could submit a link to an image with questionable content, and that would get displayed. In a sense, this could 'hurt' your site.

 

From an attack vector point of view, I don't think there aren't any image exploits on modern browsers. Any holes in your script would be huge, and affect a large percentage of sites on the internet.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.