Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php - check whether string end with image extension [duplicate]

Tags:

php

preg-match

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.

like image 450
newworroo Avatar asked Aug 07 '13 05:08

newworroo


2 Answers

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';
}
?>
like image 166
Manoj Yadav Avatar answered Oct 17 '22 19:10

Manoj Yadav


try this code,

if (preg_match('/(\.jpg|\.png|\.bmp)$/i', $aaa)) {
   echo "image";
} else{
   echo "not image";
}
like image 14
Janith Chinthana Avatar answered Oct 17 '22 18:10

Janith Chinthana