Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php检查图像文件名是否不是.jpg、.jpeg、.png或.gif。_Php_Regex_Mime Types - Fatal编程技术网

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

php检查图像文件名是否不是.jpg、.jpeg、.png或.gif。,php,regex,mime-types,Php,Regex,Mime Types,我尝试将web图像保存到本地,我在这里使用代码进行判断,如果图像文件名end不是.jpg、.jpeg、.png或.gif,请添加它们。我使用的是stripos,但是当像这样的图像url出现时,我会遇到一些麻烦。那么如何解决呢?谢谢 $webimage = 'http://pcdn.500px.net/5953805/d0dd841969187f47e8ad9157713949b4b95b3bda/4.jpg?1333782904356'; $pieces = explode("/", $webi

我尝试将web图像保存到本地,我在这里使用代码进行判断,如果图像文件名end不是.jpg、.jpeg、.png或.gif,请添加它们。我使用的是
stripos
,但是当像这样的图像url出现时,我会遇到一些麻烦。那么如何解决呢?谢谢

$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
    }
}
此功能可能会对您有所帮助。

您可以这样尝试

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

为什么不使用preg_match

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

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

希望能有所帮助!)

这适用于jpg和jpeg以及大写字母。 它也适用于文件名,如
testing.the.bicycle.jpg

试穿


仅仅因为文件名以.jpg结尾并不意味着它是jpeg文件,最好检查mime类型。这是一个很好的方法,也许应该在数组中添加
jpg,jpeg,PNG,GIF
。您必须更改文件名,而不带点。否则,请找到数组的长度$len=(sizeof($ext)),并将$ext[1]替换为$ext[$len-1],更新后的答案为
end($ext)
,它将返回数组的最后一个索引,从而只返回扩展名pathinfo是否正确处理原始示例中的get变量?您好,
(?:[\?\\\\\\\\\\\\\\\\\\\\\\].*)用于什么?我是雷格克斯的新手。谷歌搜索了几次后,才知道这是一个无法捕获的群体。但我不明白。你能告诉我吗?
$Extension=strrev(substr(strrev($fileName),0,strpos(strrev($fileName),'.')));
if(preg_match('/png|jpg|jpeg|gif/',$Extension))
{ 
    true 
}
else
{ 
    false
}
public function is_file_image($filename)
{
   $regex = '/\.(jpe?g|bmp|png|JPE?G|BMP|PNG)(?:[\?\#].*)?$/';

   return preg_match($regex, $filename);
}