php - 检查字符串是否以图像扩展名结尾

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18095379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:58:17  来源:igfitidea点击:

php - check whether string end with image extension

phppreg-match

提问by newworroo

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.

我怎么做?它将检查 100 张图像,所以它应该很快。我知道有一种文件类型验证方法,但我认为这很慢.. preg_match 怎么样?它更快吗?我不擅长 preg_match。

Thank you in advance.

先感谢您。

回答by Manoj Yadav

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';
}
?>

回答by Janith Chinthana

try this code,

试试这个代码,

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

回答by invisal

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 pathinfoto retrieve detail information about file including extension.
  • I am using strtolowerto ensure that the extension will match our list of supported image even it is in different case
  • Using in_arrayto check whether file extension is in our list of image extenion.
  • 我正在使用pathinfo检索有关文件的详细信息,包括扩展名。
  • 我正在使用strtolower以确保扩展程序将匹配我们支持的图像列表,即使它是在不同的情况下
  • 使用in_array检查文件的扩展名是否在我们的形象extenion的列表。

回答by Sivasailanathan

try this:

尝试这个:

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

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

回答by VIVEK-MDU

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 strrchrit takes last occurence of character string.. else some other concept.

strrchr它需要最后一次出现的字符串.. 其他一些其他概念。

$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 STRTOLOWERfunction and also used in_arrayfunction

在这里您可以使用STRTOLOWER函数,也可以使用in_array函数

回答by VIVEK-MDU

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:

是的,正则表达式是要走的路。或者,您可以围绕 分割"."并根据图像扩展数组检查返回数组中的最后一个元素。我不是一个 PHP 人,所以我不能为你写代码,但我可以写正则表达式:

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

^[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.

正如您在评论中看到的那样,我可能还缺少很多其他东西。只需阅读那些,看看正则表达式参考,你就会没事的。

回答by Nathan Srivi

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';
}