[PHP question]Image exists?

monoVertex

I'm back!
Reaction score
460
Ok, I want to know if there is any PHP functions that tells if a given link is an image.

Hope you get what I mean. Thanks in advance :)
 
V

Vector

Guest
You can use mime_content_type to detect the MIME type of the file, and then just test that for specific values, for example image/gif or image/jpeg [NOTE: I think even jpg files' mime types are jpeg]. For your purposes all you would want to know is if the first part of the MIME type read as "image", so you might want a snip of code like this...

Code:
$file = "myfile.png";
$type = mime_content_type($file);
if (strcmp(substr($type, 0, 6), "image/") == 0)
{
    //It's an image!
} else {
    //It's not an image!
}

Note I included the forward slash as a safeguard to make sure that was the end of the "type".

Failing that, you may find the exif_imagetype function helpful. An example is posted on that page, and so is a list of constants. I believe that if it is not an image this function will return FALSE, so you should be able to do a strict equality test.

Code:
$file = "myfile.png";
if (exif_imagetype($file) === FALSE)
{
    //It's not an image!
} else {
    //It's an image!
}

Hope that helps!
--Vector
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top