zoukankan      html  css  js  c++  java
  • 一个很好文件上传类

      其实网上已经有很多这样的类了,不过出于练手的目的还是自己仿照着写了一个。

      下面的代码放在一个名为UploadFile.class.php文件内

      1 <?php
      2     /**
      3      * 文件上传
      4      * author:师少兵
      5      * email :beibeijing163@163.com
      6      * time  :2012/12/09
      7      */
      8     class UploadFile{
      9         private $max_size      = '2000000'; //设置上传文件的大小,此为2M
     10         private $rand_name     = true;      //是否采用随机命名
     11         private $allow_type    = array();   //允许上传的文件扩展名
     12         private $error         = 0;         //错误代号
     13         private $msg           = '';        //信息
     14         private $new_name      = '';        //上传后的文件名
     15         private $save_path     = '';        //文件保存路径
     16         private $uploaded      = '';        //路径.文件名
     17         private $file          = '';        //等待上传的文件
     18         private $file_type     = array();   //文件类型
     19         private $file_ext      = '';        //上传文件的扩展名
     20         private $file_name     = '';        //文件原名称
     21         private $file_size     = 0;         //文件大小
     22         private $file_tmp_name = '';        //文件临时名称
     23         
     24         /**
     25          * 构造函数,初始化
     26          * @param string $rand_name  是否随机命名
     27          * @param string $save_path  文件保存路径
     28          * @param string $allow_type 允许上传类型
     29                 $allow_type可为数组   array('jpg', 'jpeg', 'png', 'gif');
     30                 $allow_type可为字符串 'jpg|jpeg|png|gif';中间可用' ', ',', ';', '|'分割
     31          */
     32         public function __construct($rand_name=true, $save_path='./upload/', $allow_type=''){
     33             $this->rand_name  = $rand_name;
     34             $this->save_path  = $save_path;
     35             $this->allow_type = $this->get_allow_type($allow_type);
     36         }
     37         
     38         /**
     39          * 上传文件
     40          * 在上传文件前要做的工作
     41          * (1) 获取文件所有信息
     42          * (2) 判断上传文件是否合法
     43          * (3) 设置文件存放路径
     44          * (4) 是否重命名
     45          * (5) 上传完成
     46          * @param array $file 上传文件
     47          *         $file须包含$file['name'], $file['size'], $file['error'], $file['tmp_name']
     48          */
     49         public function upload_file($file){
     50             //$this->file      = $file;
     51             $this->file_name     = $file['name'];
     52             $this->file_size     = $file['size'];
     53             $this->error         = $file['error'];
     54             $this->file_tmp_name = $file['tmp_name'];
     55             
     56             $this->ext = $this->get_file_type($this->file_name);
     57             
     58             switch($this->error){
     59                 case 0: $this->msg = ''; break;
     60                 case 1: $this->msg = '超出了php.ini中文件大小'; break;
     61                 case 2: $this->msg = '超出了MAX_FILE_SIZE的文件大小'; break;
     62                 case 3: $this->msg = '文件被部分上传'; break;
     63                 case 4: $this->msg = '没有文件上传'; break;
     64                 case 5: $this->msg = '文件大小为0'; break;
     65                 default: $this->msg = '上传失败'; break;
     66             }
     67             if($this->error==0 && is_uploaded_file($this->file_tmp_name)){
     68                 //检测文件类型
     69                 if(in_array($this->ext, $this->allow_type)==false){
     70                     $this->msg = '文件类型不正确';
     71                     return false;
     72                 }
     73                 //检测文件大小
     74                 if($this->file_size > $this->max_size){
     75                     $this->msg = '文件过大';
     76                     return false;
     77                 }
     78             }
     79             $this->set_file_name();
     80             $this->uploaded = $this->save_path.$this->new_name;
     81             if(move_uploaded_file($this->file_tmp_name, $this->uploaded)){
     82                 $this->msg = '文件上传成功';
     83                 return true;
     84             }else{
     85                 $this->msg = '文件上传失败';
     86                 return false;
     87             }
     88         }
     89         
     90         /**
     91         * 设置上传后的文件名
     92         * 当前的毫秒数和原扩展名为新文件名
     93         */
     94         public function set_file_name(){
     95             if($this->rand_name==true){
     96                 $a = explode(' ', microtime());
     97                 $t = $a[1].($a[0]*1000000);
     98                 $this->new_name = $t.'.'.($this->ext);
     99             }else{
    100                 $this->new_name = $this->file_name;
    101             }
    102         }
    103         
    104         /**
    105         * 获取上传文件类型
    106         * @param string $filename 目标文件
    107         * @return string $ext 文件类型
    108         */
    109         public function get_file_type($filename){
    110             $ext = pathinfo($filename, PATHINFO_EXTENSION);
    111             return $ext;
    112         }
    113         
    114         /**
    115          * 获取可上传文件的类型
    116          */
    117         public function get_allow_type($allow_type){
    118             $s = array();
    119             if(is_array($allow_type)){
    120                 foreach($allow_type as $value){
    121                     $s[] = $value;
    122                 }
    123             }else{
    124                 $s = preg_split("/[\s,|;]+/", $allow_type);
    125             }
    126             return $s;
    127         }
    128         
    129         //获取错误信息
    130         public function get_msg(){
    131             return $this->msg;
    132         }
    133     }
    134 ?>

      其实上面的代码中还有一个可以改进的地方,就是将那些以‘file_’开头的变量缩写为一个$file数组,这样感觉更好一些。

      下面我们来测试一下上面的代码。我在一个名为upfile.php文件写测试代码,同时将UploadFile.class.php放在同一个路径下。

     1 <html>
     2 <head>
     3     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     4     <title>upfile</title>
     5 </head>
     6 <body>
     7     <?php
     8         require 'UploadFile.class.php';
     9         if(isset($_POST['sf']) && $_POST['sf']=='sf'){
    10             if ($_FILES["file"]["error"] > 0){
    11                 echo "Error: " . $_FILES["file"]["error"] . "<br />";
    12             }else{
    13                 $file = $_FILES['file'];
    14                 
    15                 $upload = new UploadFile(true, './images/', array('jpg', 'jpeg', 'png'));
    16                 $upload->upload_file($file);
    17                 echo $upload->get_msg();
    18             }
    19         }else{
    20     ?>
    21     <form action="" method='post' enctype="multipart/form-data">
    22         <input type="file" name="file" id="file" />
    23         <input type="hidden" name="sf" value="sf"/>
    24         <input type="submit" value="上传" name="sub" />
    25     </form>
    26     <?php
    27         }
    28     ?>
    29 </body>
    30 </html>

      在上面的代码中,我们可以尝试修改第15行的参数,用来判断一下我们写的方法是否正确。

      这3个参数的含义分别表示:是否使用系统命名、文件存放的路径(相对)、允许上传的文件类型。那么就试试修改这3个参数会发生什么样的变化:(1)把true改为false是否就可以使用它原来的名字了;(2)改下存放路径,看看能不能依然能够上传;(3)试试上传几个不允许的文件,看能不能禁止住,而且关键第三个参数有两种形式,一种是数组,就像示例中一样;还有一种是字符串,用分隔符隔开就行, 'jpg|jpeg|png|gif', 'jpg jpeg png gif', 'jpg,jpeg,png,gif'都行。

      好的,文件上传类就这样写好了。

  • 相关阅读:
    docker 清理日志文件
    POJ2182 Lost Cows
    POJ3468
    楼兰图腾
    P2024 [NOI2001]食物链
    POJ1733 Parity game
    洛谷P1196 [NOI2002]银河英雄传说
    洛谷P1955 [NOI2015]程序自动分析
    CF 660 C. Uncle Bogdan and Country Happiness
    CF 660A&B
  • 原文地址:https://www.cnblogs.com/xumengxuan/p/2811783.html
Copyright © 2011-2022 走看看