Home > Backend Development > PHP Tutorial > PHP encapsulated single file upload class

PHP encapsulated single file upload class

墨辰丷
Release: 2023-03-28 17:18:02
Original
1608 people have browsed it

This article mainly introduces the single file (image) upload class encapsulated by PHP, and analyzes the related processing skills of PHP image file upload operation in the form of a complete example. Friends in need can refer to it.

The details are as follows :

<?php
//封装php中的单文件(图片)上传类
/*
//参数1:$file 文件数组 5个属性值 name,type,size,tmp,error
//参数2:文件保存的路径$path
//参数3:文件上传允许的类型 $allow数组  $allow=array(&#39;image/jpeg&#39;,&#39;image/jpg&#39;,&#39;image/png&#39;,&#39;image/gif&#39;)
//参数4: 允许文件上传的最大大小 $size
//返回值: return $imageName文件的名字
*/
header(&#39;content-type:text/html;charset=utf-8&#39;);
class Upload{
   //定义一个属性,专门保存错误信息
    public static $error;
   //文件转移的方法
  public function uploadFile($file,$size,$path){
    if(!is_dir($path)){
     $this->mkPath($path);
    }
    $allow=array(&#39;image/jpeg&#39;,&#39;image/jpg&#39;,&#39;image/png&#39;,&#39;image/gif&#39;);
   //首先判断文件是否已上传到临时目录
    if(!is_array($file)){
     Upload::$error=&#39;不是一个有效的文件&#39;;
     return false;
    }
   //判断文件是否上传到临时目录成功
   switch($file[&#39;error&#39;]){
    case 1:
      Upload::$error=&#39;上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值。&#39;;
      return false;
    case 2:
      Upload::$error=&#39;上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值。&#39;;
      return false;
    case 3:
      Upload::$error=&#39;文件只有部分被上传&#39;;
      return false;
    case 4:
      Upload::$error=&#39;没有文件被上传&#39;;
       return false;
    case 6:
      Upload::$error=&#39;找不到临时文件夹&#39;;
      return false;
    case 7:
       Upload::$error=&#39;文件写入失败&#39;;
       return false;
   }
   //判断文件类型是否是图片
    if(!in_array($file[&#39;type&#39;],$allow)){
     Upload::$error=&#39;不是要求的文件类型&#39;;
     return false;
    }
   //判断文件的大小是否在允许的范围内
    if($file[&#39;size&#39;]>$size){
     Upload::$error=&#39;超出允许最大文件大小&#39;;
     return false;
    }
    //文件转移
    if(move_uploaded_file($file[&#39;tmp_name&#39;],$path.&#39;/&#39;.$this->getName($file))){
     return $file[&#39;name&#39;];
    }else{
     return Upload::$error;
    }
  }
  //创建目录的方法
    private function mkPath($path){
    mkdir($path);
  }
  //文件重命名
    private function getName($file){
     return time().$file[&#39;name&#39;];
    }
}
Copy after login

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

Detailed explanation of the implementation steps of in_array implicit conversion in PHP

PHP detailed explanation of the heap sort algorithm

phpWhat are the methods to read local json files

The above is the detailed content of PHP encapsulated single file upload class. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template