Jump to content
MakeWebGames

Recommended Posts

Posted

Lets say that you are storing the URL address to an image file in your database, such as a users display picture, avatar, etc, that the user has added. You've already used php filter functions to make sure that the output to the image doesn't contain any harmful code and contains an image type extension, but have you checked if the image actually exists??

The best way to check if the remote image actually exists is to use a cURL.

 

You're unable to view this code.

Viewing code within this forum requires registration, you can register here for free.

 

This will send out a request to the image URL header and return true if the file exists or false if not.

However, you need to make sure that you have cURL enabled on your server. Contact your server tech to enable it.

If it is not enabled, you would get an error such as: Fatal error: Call to undefined function curl_init()

The alternative method is using the php function file_get_contents()

 

You're unable to view this code.

Viewing code within this forum requires registration, you can register here for free.

 

This will attempt to read the given file into a string, and if it is able to, return true, otherwise return false. The default is to download the entire file into a string, so make sure that the last parameter is set to 1, which only forces the function to only download 1 byte.

I have used both methods, and cURL is much faster (many blogs will say the same). Rather than downloading to your site, the cURL will just make a connection request to the header (not the body) and check that way. If you're only checking one file, the speed difference will not be noticeable. It's when you start making multiple requests that a difference becomes apparent.

 

References

http://stackoverflow.com/questions/981954/how-can-one-check-to-see-if-a-remote-file-exists-using-php

http://php.net/manual/en/function.file-get-contents.php

Posted

With this then if the result is false then you would be best replacing the image with a blank or holding image so you do not have a broken image on your site.

curl is nice I like curl but idk how many free or cheap hosts will have it enabled.

Nice examples. Personally I like to keep things in house. Secured uploading and validating rather than relying for the return from another webserver. If the webserver your calling the image from is slow or hangs then your site hangs because of it.

Posted
Try this for the url exists:

 

You're unable to view this code.

Viewing code within this forum requires registration, you can register here for free.

Not tested, should work though

This a filtering function that will only validate that the URL string is a valid link. It won't validate that the image exists on a remote server. I would use that as a pre-filter function before I ran the link through my url_exists function.

 

 

With this then if the result is false then you would be best replacing the image with a blank or holding image so you do not have a broken image on your site.

Agreed, that would be a good practice to use.

For me, I allow my players to upload a link to an external image for a few different instances, so I need the function to be a bit more flexible.

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.   Paste as plain text instead

  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...