Home >Backend Development >PHP Tutorial >How to limit the format of uploaded images?
It is stipulated that uploaded files can only be pictures. If they are other files, they cannot be uploaded. What is this restriction?
<code>if($_SERVER['REQUEST_METHOD']=='POST'){ if(is_uploaded_file($_FILES['up']['tmp_name'])){ $path='./upload/'; is_dir($path) || mkdir($path,0777,true); $type=ltrim(strchr($_FILES['up']['type'],'/'),'/'); $fileName = time().mt_rand(0, 9999).'.'.$type; $fullpath = $path . $fileName; move_uploaded_file($_FILES['up']['tmp_name'], $fullpath); } }</code>
It is stipulated that uploaded files can only be pictures. If they are other files, they cannot be uploaded. What is this restriction?
<code>if($_SERVER['REQUEST_METHOD']=='POST'){ if(is_uploaded_file($_FILES['up']['tmp_name'])){ $path='./upload/'; is_dir($path) || mkdir($path,0777,true); $type=ltrim(strchr($_FILES['up']['type'],'/'),'/'); $fileName = time().mt_rand(0, 9999).'.'.$type; $fullpath = $path . $fileName; move_uploaded_file($_FILES['up']['tmp_name'], $fullpath); } }</code>
<code><?php //可以上传的类型 $arr_file_type = ['image/gif']; //如果上传的类型 不在配置的类型数组里面 if(!in_array($_FILES['fileName']['type'],$arr_file_type)) { $msg ='文件类型有误!'; return false; } </code>
Judging by the extension, there are risks
Judge by MIME Types