如果 jpg 或 jpeg 爆炸,则获取文件

get file if jpg or jpeg in explode

如何只保存 jpg 或 jpeg 文件?

if($type!='0' and $type!='e'){
    $folder='extra/'.$folder;
}   
$ext=explode('.', $url);
$last=count($ext)-1;
$nomeFile=$dir .$extra.$folder.$name."_".$i.$suffix.'.'.$ext[$last];
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$raw=curl_exec($ch);
curl_close ($ch);
if(file_exists($nomeFile)){
  unlink($nomeFile);
}
$fp = fopen($nomeFile,'x');
   fwrite($fp, $raw);
   fclose($fp);
   return $name."_".$i.'.'.$ext[$last];
}

}

如何只保存 jpg 或 jpeg 文件?

放置一个 if 语句:

if($ext[$last] == "jpg" || $ext[$last] == "jpeg") {

$fp = fopen($nomeFile,'x');
fwrite($fp, $raw);
fclose($fp);
return $name."_".$i.'.'.$ext[$last];
}
else{
return "";
}

虽然你应该检查 mime 类型

if (function_exists('finfo_open')) {

$finfo = finfo_open(FILEINFO_MIME);
$mimetype = finfo_file($finfo, $url);
finfo_close($finfo);
echo $mimetype;
}