走过平凡人生、留下平凡足迹 注册 | 登陆

PHP 文件上传类

php代码
  1. <?php  
  2. //  
  3. // +----------------------------------------------------------------------+  
  4. // | 文件上传                                                             |  
  5. // | 本代码仅供学习讨论之用,允许随意修改                                 |  
  6. // +----------------------------------------------------------------------+  
  7. //  
  8.   
  9. $UPLOAD_CLASS_ERROR = array( 1 => '不允许上传该格式文件',  
  10.                              2 => '目录不可写',  
  11.                              3 => '文件已存在',  
  12.                              4 => '不知名错误',  
  13.   
  14. 5 => '文件太大'  
  15.                             );  
  16.   
  17. /** 
  18. * Purpose 
  19. * 文件上传 
  20. * 
  21. * Example 
  22. * 
  23.     $fileArr['file'] = $file; 
  24.     $fileArr['name'] = $file_name; 
  25.     $fileArr['size'] = $file_size; 
  26.     $fileArr['type'] = $file_type; 
  27.     // 所允许上传的文件类型 
  28.     $filetypes = array('gif','jpg','jpge','png'); 
  29.     // 文件上传目录 
  30.     $savepath = "/usr/htdocs/upload/"; 
  31.     // 没有最大限制 0 无限制 
  32.     $maxsize = 0; 
  33.     // 覆盖 0 不允许  1 允许 
  34.     $overwrite = 0; 
  35.     $upload = new upload($fileArr, $file_name, $savepath, $filetypes, $overwrite, $maxsize); 
  36.     if (!$upload->run()) 
  37.     { 
  38.      echo   $upload->errmsg(); 
  39.     } 
  40. * 
  41. * @author whxbb(whxbb@21cn.com) 
  42. * @version 0.1 
  43. */  
  44. class upload  
  45. {  
  46.     var $file;  
  47.     var $file_name;  
  48.     var $file_size;  
  49.     var $file_type;  
  50.   
  51.     /** 保存名 */  
  52.     var $savename;  
  53.     /** 保存路径 */  
  54.     var $savepath;  
  55.     /** 文件格式限定 */  
  56.     var $fileformat = array();  
  57.     /** 覆盖模式 */  
  58.     var $overwrite = 0;  
  59.     /** 文件最大字节 */  
  60.     var $maxsize = 0;  
  61.     /** 文件扩展名 */  
  62.     var $ext;  
  63.     /** 错误代号 */  
  64.     var $errno;  
  65.   
  66.     /** 
  67.      * 构造函数 
  68.      * @param $fileArr 文件信息数组 'file' 临时文件所在路径及文件名 
  69.                                     'name' 上传文件名 
  70.                                     'size' 上传文件大小 
  71.                                     'type' 上传文件类型 
  72.      * @param savename 文件保存名 
  73.      * @param savepath 文件保存路径 
  74.      * @param fileformat 文件格式限制数组 
  75.      * @param overwriet 是否覆盖 1 允许覆盖 0 禁止覆盖 
  76.      * @param maxsize 文件最大尺寸 
  77.      */  
  78.     function upload($fileArr$savename$savepath$fileformat$overwrite = 0, $maxsize = 0) {  
  79.         $this->file = $fileArr['file'];  
  80.         $this->file_name = $fileArr['name'];  
  81. $this->file_size = $fileArr['size'];  
  82.         $this->file_type = $fileArr['type'];  
  83.   
  84.         $this->get_ext();  
  85.         $this->set_savepath($savepath);  
  86.         $this->set_fileformat($fileformat);  
  87.         $this->set_overwrite($overwrite);  
  88.         $this->set_savename($savename);  
  89.         $this->set_maxsize($maxsize);  
  90.     }  
  91.   
  92.     /** 上传  */  
  93.     function run()  
  94.     {  
  95.         /** 检查文件格式 */  
  96.         if (!$this->validate_format())  
  97.         {  
  98.             $this->errno = 1;  
  99.             return false;  
  100.         }  
  101.         /** 检查目录是否可写 */  
  102.         if(!@is_writable($this->savepath))  
  103.         {  
  104.             $this->errno = 2;  
  105.             return false;  
  106.         }  
  107.         /** 如果不允许覆盖,检查文件是否已经存在 */  
  108.         if($this->overwrite == 0 && @file_exists($this->savepath.$this->savename))  
  109.         {  
  110.             $this->errno = 3;  
  111.             return false;  
  112.         }  
  113.         /** 如果有大小限制,检查文件是否超过限制 */  
  114.         if ($this->maxsize != 0 )  
  115.           
  116.   
  117.             if ($this->file_size > $this->maxsize)  
  118.             {  
  119.                 $this->errno = 5;  
  120.                 return false;  
  121.             }  
  122.         }  
  123.         /** 文件上传 */  
  124.        if(!@copy($this->file, $this->savepath.$this->savename))  
  125.        {  
  126.             $this->errno = 4;  
  127.             return false;  
  128.        }  
  129.        /** 删除临时文件 */  
  130.        $this->destory();  
  131.        return true;  
  132.     }  
  133. * 文件格式检查  
  134.      * @access protect  
  135.      */  
  136.     function validate_format()  
  137.     {  
  138.   
  139.         if (!is_array($this->fileformat))  // 没有格式限制  
  140.             return true;  
  141.        $ext = strtolower($this->ext);  
  142.         reset($this->fileformat);  
  143.         while(list($var$key) = each($this->fileformat))  
  144.         {  
  145.             if (strtolower($key) == $ext)  
  146.                 return true;  
  147.         }  
  148.         reset($this->fileformat);  
  149.         return false;  
  150.     }  
  151.   
  152.     /** 
  153.      * 获取文件扩展名 
  154.      * access public 
  155.      */  
  156.     function get_ext()  
  157.     {  
  158.         $ext = explode("."$this->file_name);  
  159.         $ext = $ext[count($ext) - 1];  
  160.         $this->ext = $ext;  
  161.     }  
  162.     /** 
  163.      * 设置上传文件的最大字节限制 
  164.      * @param $maxsize 文件大小(bytes) 0:表示无限制 
  165.      * @access public 
  166.      */  
  167.     function set_maxsize($maxsize)  
  168.     {  
  169.         $this->maxsize = $maxsize;  
  170.     }  
  171.   
  172.     /** 
  173.      * 设置覆盖模式 
  174.      * @param 覆盖模式 1:允许覆盖 0:禁止覆盖 
  175.      * @access public 
  176.      */  
  177.     function set_overwrite($overwrite)  
  178.     {  
  179.         $this->overwrite = $overwrite;  
  180.     }  
  181.   
  182.     /** 
  183.      * 设置允许上传的文件格式 
  184.      * @param $fileformat 允许上传的文件扩展名数组 
  185.      * @access public 
  186.      */  
  187.     function set_fileformat($fileformat)  
  188.     {  
  189.         $this->fileformat = $fileformat;  
  190.     }  
  191.   
  192.     /** 
  193.      * 设置保存路径 
  194.      * @param $savepath 文件保存路径:以 "/" 结尾 
  195.      * @access public 
  196.      */  
  197.     function set_savepath($savepath)  
  198.     {  
  199.         $this->savepath = $savepath;  
  200.     }  
  201.     /** 
  202.      * 设置文件保存名 
  203.      * @savename 保存名,如果为空,则系统自动生成一个随机的文件名 
  204. * @access public 
  205.      */  
  206.     function set_savename($savename)  
  207.     {  
  208.         if ($savename == '')  // 如果未设置文件名,则生成一个随机文件名  
  209.         {  
  210.             srand ((double) microtime() * 1000000);  
  211.             $rnd = rand(100,999);  
  212.             $name = date('Ymdhis') + $rnd;  
  213.             $name = $name.".".$this->ext;  
  214.         } else {  
  215.             $name = $savename;  
  216.         }  
  217.         $this->savename = $name;  
  218.     }  
  219.     /** 
  220.      * 删除文件 
  221.      * @param $file 所要删除的文件名 
  222.      * @access public 
  223.      */  
  224.     function del($file)  
  225.     {  
  226.         if(!@unlink($file))  
  227.         {  
  228.             $this->errno = 3;  
  229.             return false;  
  230.         }  
  231.         return true;  
  232.     }  
  233.     /** 
  234.      * 删除临时文件 
  235.      * @access proctect 
  236.      */  
  237.     function destory()  
  238.     {  
  239.         $this->del($this->file);  
  240.     }  
  241.   
  242.     /** 
  243.      * 得到错误信息 
  244.     * @access public 
  245.       * @return error msg string or false 
  246.      */  
  247.     function errmsg()  
  248.     {  
  249.         global $UPLOAD_CLASS_ERROR;  
  250.           
  251.         if ($this->errno == 0)  
  252.             return false;  
  253.         else  
  254.             return $UPLOAD_CLASS_ERROR[$this->errno];  
  255.     }  
  256. }  
  257. ?> 

使用示例:

PHP代码
  1. <?php  
  2. include_once "upload.class.php";  
  3. if ($Submit != '')  
  4. {  
  5.     $fileArr['file'] = $file;  
  6.     $fileArr['name'] = $file_name;  
  7.     $fileArr['size'] = $file_size;  
  8.     $fileArr['type'] = $file_type;  
  9.     /** 所允许上传的文件类型 */  
  10.     $filetypes = array('gif','jpg','jpge','png');  
  11.     /** 文件上传目录 */  
  12.     $savepath = "/usr/htdocs/upload/";  
  13.     /** 没有最大限制 0 无限制*/  
  14.     $maxsize = 0;  
  15.     /** 覆盖 0 不允许  1 允许 */  
  16.     $overwrite = 0;  
  17.     $upload = new upload($fileArr$file_name$savepath$filetypes$overwrite$maxsize);  
  18.     if (!$upload->run())  
  19.     {  
  20.      echo  "上传失败".$upload->errmsg();  
  21.     }  
  22. }  
  23. ?>  
  24. <html>  
  25. <head>  
  26. <title>文件上传</title>  
  27. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">  
  28. </head>  
  29.   
  30. <body bgcolor="#FFFFFF" text="#000000">  
  31. <form name="form1" enctype="multipart/form-data" method="post" action="">  
  32.   <input type="file" name="file">  
  33.   <input type="submit" name="Submit" value="Submit">  
  34. </form>  
  35. </body>  
  36. </html>  

 

 

 

 

Tags: php, 技巧, 教程

« 上一篇 | 下一篇 »

Trackbacks

点击获得Trackback地址,Encode: UTF-8 点击获得Trackback地址,Encode: GB2312 or GBK 点击获得Trackback地址,Encode: BIG5

发表评论

评论内容 (必填):