Home  >  Article  >  Backend Development  >  How to limit the format of uploaded images?

How to limit the format of uploaded images?

WBOY
WBOYOriginal
2016-09-12 17:44:491072browse

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>

Reply content:

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>

  1. Judging by the extension, there are risks

  2. Judge by MIME Types

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn