检查PHP中图片文件名是否以.jpg、.jpeg、.png或.gif结尾。

4

我尝试将网页图片保存到本地,我使用下面的代码来判断是否需要添加 .jpg, .jpeg, .png 或 .gif 的文件扩展名。我使用了 stripos 函数,但是当遇到这种形式的图片链接时遇到了一些问题。那么该如何解决呢?谢谢。

$webimage = 'http://pcdn.500px.net/5953805/d0dd841969187f47e8ad9157713949b4b95b3bda/4.jpg?1333782904356';
$pieces = explode("/", $webimage); 
$pathend = end($pieces);
$imageinfo = @getimagesize($webimage);
$imagetype= $imageinfo['mime'];
if($imagetype=='image/jpeg'){
    if(stripos($pathend,'.jpg')==0){
        $newpathend = $pathend.'.jpg'; // if image end is't '.jpg', add '.jpg'
    }else if(stripos($pathend,'.jpeg')==0){
        $newpathend = $pathend.'.jpeg'; // if image end is't '.jpg', add '.jpeg'
    }else{
        $newpathend = $pathend;// if image end is '.jpg' or '.jpeg', do not change
    }
}
if($imagetype=='image/png'){
    if(stripos($pathend,'.png')==0){
        $newpathend = $pathend.'.png'; // if image end is't '.png', add '.png'
    }else{
        $newpathend = $pathend;// if image end is '.png', do not change
    }
}
if($imagetype=='image/gif'){
    if(stripos($pathend,'.gif')==0){
        $newpathend = $pathend.'.gif'; // if image end is't '.gif', add '.gif'
    }else{
        $newpathend = $pathend;// if image end is '.gif', do not change
    }
}

1
仅仅因为文件名以 .jpg 结尾并不意味着它是一个 JPEG 文件,最好检查 MIME 类型。 - user557846
7个回答

7
你可以尝试这样做。
$type=Array(1 => 'jpg', 2 => 'jpeg', 3 => 'png', 4 => 'gif'); //store all the image extension types in array

$imgname = ""; //get image name here
$ext = explode(".",$imgname); //explode and find value after dot

if(!(in_array($ext[1],$type))) //check image extension not in the array $type
{
    //your code here
}

1
这是一个很好的方法,也许应该在数组中添加“JPG,JPEG,PNG,GIF”。 - fish man
1
你必须更改文件名,不能包含点。否则,找到数组的长度$len=(sizeof($ext)),并将$ext[1]替换为$ext[$len-1]。 - nithi
更新答案,使用 end($ext) 可以返回数组的最后一个索引,因此只返回扩展名。 - Anuj TBE

6
为什么不使用preg_match函数?
if( preg_match('/\.(jpg|jpeg|png|gif)(?:[\?\#].*)?$/i', $webimage, $matches) ) {
    // matching file extensions are in $matches[1]
}

1
这适用于jpg和jpeg格式,对于大写字母同样有效。它还适用于文件名如testing.the.bicycle.jpg
试试https://regex101.com/r/gI8uS1/1
public function is_file_image($filename)
{
   $regex = '/\.(jpe?g|bmp|png|JPE?G|BMP|PNG)(?:[\?\#].*)?$/';

   return preg_match($regex, $filename);
}

你好, (?:[\?\#].*)? 是用来做什么的?我是一个正则表达式的新手。经过一些搜索,我知道它是一个非捕获组。但是我还是不理解它。你能告诉我吗? - mrtpk

1

这个函数 pathinfo 可能会对你有所帮助。


pathinfo 能够像原始示例那样正确处理获取变量吗? - kingjeffrey

0

使用strops

if (strpos($your_text,'.png') !== false) {
        //do something
 } else if (strpos($your_text,'.jpg') !== false) {
        //do something
 } else if (strpos($your_text,'.gif') !== false) {
        //do something
 }

希望它能有所帮助!)

0

参考自本页kingjeffrey的回答

if( preg_match('/\.(jpg)(?:[\?\#].*)?$/i', $imgname, $matches) ) {
    echo "jpg";
}else if( preg_match('/\.(png)(?:[\?\#].*)?$/i', $imgname, $matches) ) {
    echo "png";
}else if( preg_match('/\.(gif)(?:[\?\#].*)?$/i', $imgname, $matches) ) {
    echo "gif";
}

-1
$Extension=strrev(substr(strrev($fileName),0,strpos(strrev($fileName),'.')));
if(preg_match('/png|jpg|jpeg|gif/',$Extension))
{ 
    true 
}
else
{ 
    false
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接