PHP群:95885625 Hbuilder+MUI群:81989597 站长QQ:634381967
    您现在的位置: 首页 > 开发编程 > PHP教程 > 正文

    php文件上传类

    作者:admin来源:B5教程网浏览:时间:2020-09-30 00:07:50我要评论
    导读:php文件上传类,总结分享给大家。
     
    复制代码代码如下:
    1. <?PHP
    2. /**
    3.  * Uploader
    4.  * 
    5.  * @package my
    6.  * @author php100.com
    7.  * @copyright 2010
    8.  * @version $Id$
    9.  * @access public
    10.  */
    11. class Uploader {
    12.  
    13.     public $files = array();
    14.     private $errors = array();
    15.     private $file_names = array();
    16.     private $directory = NULL;
    17.     private $uploaded = false;
    18.    
    19.     # uploaded file name
    20.     public $uploaded_files = array();
    21.     private $file_new_name = NULL;
    22.     private $results = NULL;
    23.  
    24.     /**
    25.      * set directory attribute
    26.      * 
    27.      * @param mixed $directory
    28.      * @return
    29.      */
    30.     function directory($directory) {
    31.         if (!is_dir($directory)) {
    32.             $this -> errors []= $directory.' not a directory';
    33.         }
    34.         if (!is_writable($directory)) {
    35.             $this -> errors []= $directory.' unwritable';
    36.         }
    37.         $this -> directory = $directory;
    38.     }
    39.  
    40.     /**
    41.      * set files attribute
    42.      * 
    43.      * @param mixed $files
    44.      * @return
    45.      */
    46.     function files($files)
    47.     {
    48.         if(empty($files)) {
    49.             throw new Exception('file array is empty');
    50.         }
    51.         if (!is_array($files['tmp_name'])) {
    52.             $this -> files['tmp_name'][] = $files['tmp_name'];
    53.             $this -> files['name'][] = $files['name'];
    54.             $this -> files['type'][] = $files['type'];
    55.             $this -> files['size'][] = $files['size'];
    56.         }
    57.         else {
    58.             for ($= 0; $< count($files['name']); $i++) {
    59.                 if ($files['name'][$i]) {
    60.                     $this -> files['tmp_name'][] = $files['tmp_name'][$i];
    61.                     $this -> files['name'][] = $files['name'][$i];
    62.                     $this -> files['type'][] = $files['type'][$i];
    63.                     $this -> files['size'][] = $files['size'][$i];
    64.                 }
    65.             }
    66.         }
    67.     }
    68.  
    69.     /**
    70.      * Uploader::bad_character_rewrite()
    71.      * 
    72.      * @param mixed $text
    73.      * @return
    74.      */
    75.     private function bad_character_rewrite($text)
    76.     {
    77.             $first = array("\\", "/", ":", ";", "~", "|", "(", ")", "\"", "#", "*", "$", "@", "%", "[", "]", "{", "}", "<", ">", "`", "'", ",", " ", "臒", "臑", "眉", "脺", "艧", "艦", "谋", "陌", "枚", "脰", "莽", "脟");
    78.             $last = array("_", "_", "_", "_", "_", "_", "", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "", "_", "_", "g", "G", "u", "U", "s", "S", "i", "I", "o", "O", "c", "C");
    79.             $text_rewrite = str_replace($first, $last, $text);
    80.             return $text_rewrite;
    81.     }
    82.  
    83.     /**
    84.      * get file extension
    85.      * 
    86.      * @param mixed $file_name
    87.      * @return
    88.      */
    89.     public function file_extension($file_name)
    90.     {
    91.         $file_extension = strtolower(substr(strrchr($file_name, '.'), 1));
    92.         return $file_extension;
    93.     }
    94.    
    95.     private function file_name($file)
    96.     {
    97.         return strtolower(substr($file, 0, strpos($file, '.')));
    98.     }
    99.  
    100.     /**
    101.      * create random file name
    102.      * 
    103.      * @param mixed $file_name  no used
    104.      * @return
    105.      */
    106.     private function _file_name_control($file_name)
    107.     {
    108.         if(file_exists($this->directory.'/'.$file_name)) {
    109.             //return $this->file_name($file_name).strtolower(rand() % 1000000).'.'.$this->file_extension($file_name);
    110.             return strtolower(rand() % 1000000).'_'.$file_name;
    111.         }
    112.         else
    113.             return $file_name;
    114.     }
    115.  
    116.     /**
    117.      * Uploader::_begin_upload()
    118.      * 
    119.      * @param mixed $extensions
    120.      * @return void
    121.      */
    122.     function _begin_upload($extensions)
    123.     {
    124.         if(!count($this->errors)) {
    125.             for ($= 0; $< count($this -> files['tmp_name']); $i++)
    126.             {
    127.                 if (in_array($this->file_extension($this->files['name'][$i]), $extensions))
    128.                 {
    129.                     $this->file_new_name = $this ->_file_name_control($this-> files['name'][$i]);
    130.                     move_uploaded_file($this -> files['tmp_name'][$i], $this-> directory.'/'.$this -> file_new_name);
    131.                     $this -> uploaded_files[] = $this -> file_new_name;
    132.                     $this -> results .= '
    133. '.$this -> files['name'][$i].' change name to '.$this -> file_new_name.', file size
      (~'.round($this -> files['size'][$i] / 1024, 2).' kb).
    134. ';
    135.                 }
    136.                 else
    137.                 {
    138.                     $this -> results .= '
    139. '.$this -> files['name'][$i].' no access type
    140. ';
    141.                 }
    142.             }
    143.             $this -> uploaded = true;
    144.         } else {
    145.             foreach($this->errors as $error) {
    146.                 throw new Exception($error);
    147.             }
    148.         }
    149.     }
    150.  
    151.     /**
    152.      * print report
    153.      * 
    154.      * @return
    155.      */
    156.     function result_report()
    157.     {
    158.         if ($this -> uploaded == true) {
    159.             echo '
      • ';
        •             echo $this -> results;
        •             echo '
        ';
      •         }
      •     }
      •  
      •     /**
      •      * Uploader::upload()
      •      * 
      •      * @param mixed $files
      •      * @param mixed $directory
      •      * @param mixed $extensions
      •      * @return
      •      */
      •     function upload($files, $directory, $extensions=array('jpg', 'jpeg', 'png', 'gif'))
      •     {
      •         $this -> directory($directory);
      •         $this -> files($files);
      •         $this -> _begin_upload($extensions);
      •     }
      • }

    转载请注明(B5教程网)原文链接:https://b5.mxunkeji.com/content-10-183-1.html
    相关热词搜索: 文件上传