zoukankan      html  css  js  c++  java
  • PHP之upload

      1 <?php
      2 
      3 
      4 class Upload{
      5 
      6     private $_upload_path; //上传路径    
      7     private $_prefix;      //文件前缀
      8     private $_ext_list;        //允许的扩展名列表
      9     private $_mime_list;   //允许的mime列表
     10     private $_max_size;   //允许的最大尺寸
     11     private $_error_info;   //上传出错信息
     12 
     13     //内部封装 只提供对外访问接口 体现程序的严谨 和 封装性。
     14     public function getErrorInfo(){
     15         return $this->_error_info;
     16     }
     17 
     18     //扩展名和MIME的映射列表
     19     private $_ext_map_mime = array(
     20         '.jpg'     => 'image/jpeg',
     21         '.jpeg' => 'image/jpeg',
     22         '.png'     => 'image/png',
     23         '.gif'  => 'image/gif'
     24         //还有很多
     25     );
     26 
     27     public function __construct(){
     28         //初始化属性
     29         $this->_initConfig();
     30     }
     31 
     32     private function _initConfig(){
     33         //指定默认值
     34         $this->_upload_path = './';
     35         $this->prefix = '';
     36         $this->_ext_list=array('.jpeg','.jpg','.gif','.png');
     37         $this->_initMimeList(); //根据配置好的扩展名列表 计算
     38         $this->_max_size = 1024*1024;  // 1M
     39     }
     40 
     41     private function _initMimeList(){
     42         foreach ($this->_ext_list as $ext) {
     43             $this->_mime_list = $this->_ext_map_mime[$ext];
     44         }
     45     }
     46 
     47     public function setUploadPath($path){
     48         if (is_dir($path)) {
     49             $this->_upload_path = $path;
     50         }else{
     51             trigger_error('设置的路径不存在');
     52         }
     53     }
     54 
     55     public function setExtList($ext_list){
     56         $this->_ext_list = $ext_list;
     57         $this->_initMimeList();
     58     }
     59 
     60     public function setMaxSize($size){
     61         $this->_max_size = $size;
     62     }
     63 
     64     public function setPrefix($prefix){
     65         $this->_prefix = $prefix;
     66     }
     67 
     68     
     69     //通过文件信息 ,判断文件是否合理,然后移动文件位置
     70     //@param array $fileinof 五个元素
     71     public function uploadFIle($fileinfo = array()){
     72         //先判断合理性
     73         //是否有错误
     74 
     75         if (0 != $fileinfo['error'] ) { // =0 上传成功
     76             $this->_error_info = '文件上传出现错误';
     77             return false;
     78         }
     79 
     80         //类型 ,通过后缀名 允许的后缀名
     81         $ext_list = $this->_ext_list;
     82 
     83         //获取后缀名 strrchr() 是为了判断 类型是否存在 程序内部定义的
     84 
     85         $ext = strrchr($fileinfo['name'], '.');
     86         //转换小写 同步程序内部的 。
     87         if (! in_array(strtolower($ext), $ext_list)) {
     88              $this->_error_info = '文件[后缀名]类型错误';
     89              return false;
     90          } 
     91 
     92          //依据MIME类型判断
     93          $mime_list = $this->_mime_list;
     94          if (! in_array($fileinfo['type'], $mime_list)) {
     95              $this->_error_info = '文件[MIME]类型错误';
     96              return false;
     97          }
     98 
     99          //php获取的类型检测
    100          //获取一个可以用来获得文件的MIME类型的Finfo的对象
    101          //从 文件内容来检测是否是 危害的文件  防止脚本程序恶意攻击
    102          $finfo = new Finfo(FILEINFO_MIME_TYPE);
    103          $mime = $finfo->file($fileinfo['tmp_name']);
    104          if (! in_array($mime, $mime_list)) {
    105              $this->_error_info = '文件[PHP 检测]类型错误';
    106              return false;
    107          }
    108 
    109          //允许的大小
    110          $max_size = $this->_max_size;
    111          if ($fileinfo['size'] >$max_size) {
    112              $this->_error_info = '文件超过限制';
    113              return false;
    114          }
    115 
    116          //文件符合以上规定 移动
    117          $upload_path = $this->_upload_path; //默认的上传路径
    118          //分配子目录
    119          $sub_dir  = date('Ymd') . '/'; //获得当前对应的子目录
    120          if (! is_dir($upload_path.$sub_dir)) { //上传目录位置
    121              mkdir($upload_path.$sub_dir);  //如果不存在 创建
    122          }
    123 
    124          //为目标文件命名
    125          $prefix = $this->_prefix;
    126          $dest_filename = uniqid($prefix, true).$ext;
    127          //文件移动到子目录内
    128          $dest_filename = $sub_dir.$dest_filename;
    129 
    130          //检测临时文件是否为HTTP上传的文件
    131          if (! is_uploaded_file($fileinfo['tmp_name'])) {
    132              $this->_error_info = '上传的临时文件有错误';
    133              return false;
    134          }
    135 
    136          //移动
    137          $result = move_uploaded_file($fileinfo['tmp_name'],$dest_filename);
    138          if ($result) {
    139              return $dest_filename; //
    140          }else{
    141              $this->_error_info = '移动失败';
    142              return  false;
    143          }
    144 
    145     }
    146 
    147 }
    148 
    149 $test = new Upload(array());
    150 $res = $test->uploadFIle();
    151 var_dump($res);
  • 相关阅读:
    DesignPattern系列__10单例模式
    DesignPattern系列__09设计模式概述
    DesignPattern系列__08UML相关知识
    DesignPattern系列__07合成复用原则
    DesignPattern系列__06迪米特原则
    爬取猫眼电影top100电影
    安卓微信对接H5微信支付出现“商家参数有误,请联系商家解决”的问题处理
    python 通过使用pandas的实现的Excel的批量转换CSV文件的处理
    输入一个字符串,判断字符串中最大对称字串的长度
    面向对象六大设计原则(转载)
  • 原文地址:https://www.cnblogs.com/sharecorner/p/6183545.html
Copyright © 2011-2022 走看看