我需要验证字符串是否为图片文件名。php - 检查字符串是否以图片扩展名结尾
$aaa = 'abskwlfd.png';
if ($aaa is image file) {
echo 'it's image';
else {
echo 'not image';
}
我该怎么做?它会拍摄100张图像,所以它应该很快。我知道有一种文件类型验证方法,但我认为这很慢.. preg_match怎么样?速度更快吗? 我不擅长preg_match。
预先感谢您。
我需要验证字符串是否为图片文件名。php - 检查字符串是否以图片扩展名结尾
$aaa = 'abskwlfd.png';
if ($aaa is image file) {
echo 'it's image';
else {
echo 'not image';
}
我该怎么做?它会拍摄100张图像,所以它应该很快。我知道有一种文件类型验证方法,但我认为这很慢.. preg_match怎么样?速度更快吗? 我不擅长preg_match。
预先感谢您。
试试这个:
<?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';
}
?>
它不适用于“abskwlfd.PNG”。应该使用'strtolower'来确保不区分大小写。 – invisal
@invisal感谢您指出,在回答中添加了“strtolower” –
是的,正则表达式是要走的路。或者,您可以围绕"."
进行拆分,并根据图像扩展名数组检查返回数组中的最后一个元素。我不是一个PHP的家伙,所以我不能给你写的代码,但是我可以写正则表达式:
^[a-zA-Z\.0-9_-]+\.([iI][mM][gG]|[pP][nN][gG]|etc....)$
这是一个相当简单的。我知道你没有用正则表达式太多的经验,但在这里就是这样一个作用:
^: 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
,如果你想匹配任何序列,然后,而不是在支架和+的东西,只需使用:
.*
“。”匹配任何字符,“*”表示它的任何数量。所以这只是基本上说“没有限制”(除了换行符)
可能有很多其他的东西我错过了,你可以在评论中看到。只要阅读这些,看看正则表达式的参考,你会没事的。
一个小错误:“您可以分割”'。“'并检查**最后一个元素**” – invisal
文件名不限于'[ a-zA-Z \ .0-9 _-]'... –
确实如此,但它是与操作系统相关的。那些几乎肯定会存在于每个操作系统中的。 – 2013-08-07 05:43:29
试试这个
$ext = pathinfo($file_name, PATHINFO_EXTENSION); case sensitive
if (in_array($ext, $supported_image)) {
echo "it's image";
} else {
echo 'not image';
}
谁说这是一个上传?这样写的方式,图像将显示它没有的每个文件扩展名的错误。 –
试试这个:
$a=pathinfo("example.exe");
var_dump($a['extension']);//returns exe
非常简单,但我如何使用它来if语句? – newworroo
试试这个
$allowed = array(
'.jpg',
'.jpeg',
'.gif',
'.png',
'.flv'
);
if (!in_array(strtolower(strrchr($inage_name, '.')), $allowed)) {
print_r('error message');
}else {
echo "correct image";
}
或strrchr需要ch的最后一次出现字符串.. 其他一些其他的概念。
$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";
}
在这里你可以使用STRTOLOWER功能,并且也使用in_array功能
试试这个代码,
if (preg_match('/(\.jpg|\.png|\.bmp)$/i', $aaa)) {
echo "image";
} else{
echo "not image";
}
这非常干净,但我意识到preg_match比使用pathinfo进行多个图像处理的方法要慢。 – newworroo
也许您正在寻找这样的:
function isImageFile($file) {
$info = pathinfo($file);
return in_array(strtolower($info['extension']),
array("jpg", "jpeg", "gif", "png", "bmp"));
}
pathinfo
来检索有关文件的详细信息,包括扩展名。strtolower
,以保证扩展将符合我们所支持的图像列表,甚至是在不同的情况下in_array
检查文件的扩展名是否在我们的形象extenion的列表。
另请参见:[PHP我如何检查一个文件是MP3还是图像文件?](http://stackoverflow.com/questions/2006632/php-how-can-i-check-if-a - 文件是MP3或图像文件)或[如何检查上传的文件是否是一个没有MIME类型的图像?](http://stackoverflow.com/questions/6484307/how-to-check-if -an-uploaded-file-is-an-image-without-mime-type) –