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.

newworroo
  • 399
  • 2
  • 3
  • 9
  • See also: [PHP how can i check if a file is mp3 or image file?](http://stackoverflow.com/questions/2006632/php-how-can-i-check-if-a-file-is-mp3-or-image-file) or [How to check if an uploaded file is an image without mime type?](http://stackoverflow.com/questions/6484307/how-to-check-if-an-uploaded-file-is-an-image-without-mime-type) – Wesley Murch Aug 07 '13 at 05:35

7 Answers7

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';
}
?>
Manoj Yadav
  • 6,560
  • 1
  • 23
  • 21
14

try this code,

if (preg_match('/(\.jpg|\.png|\.bmp)$/i', $aaa)) {
   echo "image";
} else{
   echo "not image";
}
evandrix
  • 6,041
  • 4
  • 27
  • 38
Janith Chinthana
  • 3,792
  • 2
  • 27
  • 54
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.
invisal
  • 11,075
  • 4
  • 33
  • 54
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

VIVEK-MDU
  • 2,483
  • 3
  • 36
  • 63
1

try this:

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

var_dump($a['extension']);//returns exe
Sivasailanathan
  • 135
  • 2
  • 11
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.

  • A small correct: "you could split around the `"."` and check the **last element**" – invisal Aug 07 '13 at 05:40
  • Files name aren't limited to `[a-zA-Z\.0-9_-]`... – Wesley Murch Aug 07 '13 at 05:41
  • True, but it's OS-relative. Those are the ones that will almost certainly be in every OS. –  Aug 07 '13 at 05:43
  • It's fine if you aren't worried about integrity (and don't have spaces, commas, etc.). `pathinfo()` is better and much easier. – Wesley Murch Aug 07 '13 at 05:44
  • 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. –  Aug 07 '13 at 05:45
  • Something like `.*\.(img|png|gif|etc)` I guess, all that matters are the last bits. But I wouldn't bother with regex, there are even string manipulation methods that are simpler. Sorry, I don't like the answer. – Wesley Murch Aug 07 '13 at 05:47
  • And you need to make sure that extension is case-insensitive. ".PNG", ".png", ".Png", ".PNg", .... – invisal Aug 07 '13 at 05:58
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';
}
Padmanathan J
  • 4,614
  • 5
  • 37
  • 75
  • 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. – Wesley Murch Aug 07 '13 at 05:50