11

I need to verify string whether the string is image file name.

$aaa = 'abskwlfd.png';

if ($aaa is image file) {
echo 'it's image';
else {
echo 'not image';
}

How do i do that? It will chck 100 images, so it should be fast. I know there is a filetype verification method, but I think that's slow.. What about preg_match? Is it faster? I'm not good at preg_match.

Thank you in advance.

1

7 Answers 7

38

Try this:

<?php
$supported_image = array(
    'gif',
    'jpg',
    'jpeg',
    'png'
);

$src_file_name = 'abskwlfd.PNG';
$ext = strtolower(pathinfo($src_file_name, PATHINFO_EXTENSION)); // Using strtolower to overcome case sensitive
if (in_array($ext, $supported_image)) {
    echo "it's image";
} else {
    echo 'not image';
}
?>
Sign up to request clarification or add additional context in comments.

3 Comments

It won't work with "abskwlfd.PNG". Should use strtolower to ensure case-insensitive.
@invisal Thanks for pointing out, strtolower added in answer
This repo has a list of possible image extensions, github.com/arthurvr/image-extensions/blob/master/…
14

try this code,

if (preg_match('/(\.jpg|\.png|\.bmp)$/i', $aaa)) {
   echo "image";
} else{
   echo "not image";
}

2 Comments

This is very clean, but I realized preg_match is slower than the method using pathinfo for multiple images processing..
very simple and helpful
4

Maybe you are looking for this:

function isImageFile($file) {
    $info = pathinfo($file);
    return in_array(strtolower($info['extension']), 
                    array("jpg", "jpeg", "gif", "png", "bmp"));
}
  • I am using pathinfo to retrieve detail information about file including extension.
  • I am using strtolower to ensure that the extension will match our list of supported image even it is in different case
  • Using in_array to check whether file extension is in our list of image extenion.

Comments

2

try this

 $allowed = array(
    '.jpg',
    '.jpeg',
    '.gif',
    '.png',
    '.flv'
    );
   if (!in_array(strtolower(strrchr($inage_name, '.')), $allowed)) {
     print_r('error message');
    }else {
       echo "correct image";
    }

or strrchr it takes last occurence of character string.. else some other concept.

$allowed = array(
                'image/jpeg',
                'image/pjpeg',
                'image/png',
                'image/x-png',
                'image/gif',
                'application/x-shockwave-flash'
                        );
        if (!in_array($image_name, $allowed)) {
         print_r('error message');
        }else {
           echo "correct image";
        }

Here you can used STRTOLOWER function and also used in_array function

Comments

1

try this:

$a=pathinfo("example.exe");

var_dump($a['extension']);//returns exe

1 Comment

Very simple, but how do i use it to if statement?
0

Yes, regex is the way to go. Alternatively, you could split around the "." and check the last element in the returned array against an array of image extensions. I'm not a PHP guy so I can't write the code for you, but I can write the regex:

^[a-zA-Z\.0-9_-]+\.([iI][mM][gG]|[pP][nN][gG]|etc....)$

This one is fairly simple. I know you don't have much experience with regex, but here's what this one does:

^: start of string
[a-zA-Z\.0-9_-]: describes range of characters including all letters, numbers, and ._-
\.: "." character
([iI][mM][gG]|[pP][nN][gG]|etc....): | means or. So just put all image extensions you know here. Again, the brackets for case-insensitivity

if you want to match any sequence then instead of the stuff in the brackets and the +, just use:

.*

"." matches any character and "*" means any amount of it. So this just basically says "no restrictions" (except newlines)

There are probably a lot of other things I'm missing, as you can see in the comments. Just read those, look at a regex reference, and you'll be fine.

7 Comments

A small correct: "you could split around the "." and check the last element"
Files name aren't limited to [a-zA-Z\.0-9_-]...
True, but it's OS-relative. Those are the ones that will almost certainly be in every OS.
It's fine if you aren't worried about integrity (and don't have spaces, commas, etc.). pathinfo() is better and much easier.
Like I said, I don't know much about PHP. If there's a better regex you know then I'd be glad to add it.
|
0

Try this

use pathinfo():

$ext = pathinfo($file_name, PATHINFO_EXTENSION); case sensitive
if (in_array($ext, $supported_image)) {
    echo "it's image";
} else {
    echo 'not image';
}

1 Comment

Who said it was an upload? The way this is written, the image will show an error for every file extension it doesn't have.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.