Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.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处于爆炸状态,则获取文件_Php_Explode - Fatal编程技术网

Php 如果jpg或jpeg处于爆炸状态,则获取文件

Php 如果jpg或jpeg处于爆炸状态,则获取文件,php,explode,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_HEAD

如何仅保存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($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];
}

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

实际上,您根本不应该查看文件扩展名。您应该查看
图像/jpeg
内容类型
响应标题。解释为什么要检查mime类型:如果有人将PHP脚本上载到服务器,重命名为
whater.jpg
,然后您将其作为某个
加载,因为您的代码认为它是一个图像文件,猜猜你的服务器会做什么?(提示:是的,它将运行该文件,并具有服务器端权限,执行任何操作,从删除所有文件到向某人提供进入服务器的后门),而不是“还”,当涉及用户提交的内容时,绝对不要检查扩展名是非常重要的。这使得人们可以上传一个实际上是php脚本的“图像”文件,然后在网站上以
元素的形式加载它的那一刻,BAM。后门的。超级风!绝对不要这样做。