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

How to limit the format of uploaded images?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-09-12 17:44:49
Original
1147 people have browsed it

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

<code><?php

//可以上传的类型
$arr_file_type = ['image/gif'];
//如果上传的类型 不在配置的类型数组里面
if(!in_array($_FILES['fileName']['type'],$arr_file_type))
{
    $msg ='文件类型有误!';
    return false;
}

</code>
Copy after login

  1. Judging by the extension, there are risks

  2. Judge by MIME Types

Related labels:
php
Statement of this Website
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template