如果jpg或jpeg在爆炸中获取文件

时间:2015-03-22 16:30:40

标签: php 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文件,我怎么能保存文件?

1 个答案:

答案 0 :(得分:0)

发表一个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;
}