Tuesday, May 22, 2012

How to check if an image exist on the remover server in PHP

If you are using a remote image or file in you PHP application and like to make sure image exist before using the image. It will help you keep you application clean if the image exist then you  can show the image and if the image is removed that you don't have any control over, you can show the placeholder image or don't show any image.

So how it's done.




function Check_Image($url)
{
 if(@file_get_contents($url,0,NULL,0,1))
 {
  return 1;
 }
 else
 {
  return 0;
 }
}


This function is fast when it comes to checking the image file or any other file.

So we are passing in variable called $url in this function. that is then passed into the File_Get_Contents function and it only get 1 bit from the file. If file_get_contents get 1 bit then this function will return 1 else it will return 0. you can use that in you code to show the image or don't it's you call.

No comments:

Post a Comment