简体   繁体   English

php 检查图像文件名结尾是否为.jpg、.jpeg、.png 或.gif。

[英]php check if a image file name end is not .jpg, .jpeg, .png or .gif.

I tried to save web image to local, I use code here to make a judge, if image file name end is not.jpg, .jpeg, .png or.gif, add them.我试过把web图片保存到本地,我这里用代码判断,如果图片文件名结尾不是.jpg,.jpeg,.png,.gif,就加上。 I use stripos , but I meet some trouble when an image url like this.我使用stripos ,但是当像这样的图像 url 时我遇到了一些麻烦。 So how to solve?那么如何解决呢? Thanks.谢谢。

$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
    }
}

You can try like this你可以这样试试

$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
}

Why not use preg_match?为什么不使用 preg_match?

if( preg_match('/\.(jpg|jpeg|png|gif)(?:[\?\#].*)?$/i', $webimage, $matches) ) {
    // matching file extensions are in $matches[1]
}

This function pathinfo may help you.这个 function pathinfo可能对你有帮助。

This works for both jpg as well as jpeg and with uppercase letters.这适用于 jpg 和 jpeg 以及大写字母。 It also works with filenames like testing.the.bicycle.jpg它还适用于testing.the.bicycle.jpg等文件名

Give a try on https://regex101.com/r/gI8uS1/1试试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);
}

Use strops使用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
 }

Hope it will help!)希望它会有所帮助!)

refered from kingjeffrey answered in this page引用自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";
}
$Extension=strrev(substr(strrev($fileName),0,strpos(strrev($fileName),'.')));
if(preg_match('/png|jpg|jpeg|gif/',$Extension))
{ 
    true 
}
else
{ 
    false
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM