php 文件上传实例

 

View Code
 1 <?php
 2 $return = '<?xml version="1.0" encoding="UTF-8"?>';
 3 $status = 1;
 4 $msg = '上传成功';
 5 
 6 /**
 7  * 配置"文件路径"
 8  */
 9 $upFileConfig = array(
10         "uploadPath" => "upload/"  //保存路径
11 );
12 
13 $fileName = "";
14 $path = $upFileConfig[ 'uploadPath' ];
15 if ( !file_exists( $path ) ) {
16     if (!mkdir( "$path" , 0777 )){
17         $status = 0;
18         $msg = '没有权限创建指定目录,联系管理员?';
19     }
20 }
21 
22 @$clientFile = $_FILES["upfile"];
23 if($status == 1 && !isset($clientFile)){
24     $msg = '文件大小超出服务器配置或者未选择文件上传!';
25     $status = 0;
26 }
27 
28 if ($status == 1 && $clientFile['error'] > 0){
29     $status = 0;
30     switch ($clientFile['error']){
31         case 1:
32             $msg = '文件太大';
33             break;
34         case 2:
35             $msg = '文件大小超过浏览器的限制';
36             break;
37         case 3:
38             $msg = '文件部分上传';
39             break;
40         case 4:
41             $msg = '没有找到要上传的文件!';
42             break;
43         case 5:
44             $msg = '服务器临时文件夹丢失,请重新上传!';
45             break;
46         case 6:
47             $msg = '文件写入到临时文件夹出错!';
48             break;
49 
50         default:$msg = '';
51     }
52 }
53 
54 //保存文件
55 if ( $status == 1 ) {
56     $tmp_file = $clientFile[ "name" ];
57     $result = move_uploaded_file( $clientFile[ "tmp_name" ] , $path . $clientFile['name'] );
58     if ( !$result ) {
59         $status = 0;
60         $msg = "没有权限在指定目录存放文件,联系管理员?";
61     }
62 }
63 /**
64  *返回xml,result.msg表示结果信息,result.status是状态标志,1表示上传成功,0表示上传失败
65  */
66 $return .= "<result><status>$status</status><msg>$msg</msg></result>";
67 echo $return;
68 ?>

 

一个文件上传类:

View Code
  1 <?php
  2 /**
  3  * 文件上传类
  4  */
  5 class uploadFile {
  6 
  7     public $max_size = '1000000';//设置上传文件大小
  8     public $file_name = 'date';//重命名方式代表以时间命名,其他则使用给予的名称
  9     public $allow_types;//允许上传的文件扩展名,不同文件类型用“|”隔开
 10     public $errmsg = '';//错误信息
 11     public $uploaded = '';//上传后的文件名(包括文件路径)
 12     public $save_path;//上传文件保存路径
 13     private $files;//提交的等待上传文件
 14     private $file_type = array();//文件类型
 15     private $ext = '';//上传文件扩展名
 16 
 17     /**
 18      * 构造函数,初始化类
 19      * @access public
 20      * @param string $file_name 上传后的文件名
 21      * @param string $save_path 上传的目标文件夹
 22      */
 23     public function __construct($save_path = './upload/',$file_name = 'date',$allow_types = '') {
 24         $this->file_name   = $file_name;//重命名方式代表以时间命名,其他则使用给予的名称
 25         $this->save_path   = (preg_match('//$/',$save_path)) ? $save_path : $save_path . '/';
 26         $this->allow_types = $allow_types == '' ? 'jpg|gif|png|zip|rar' : $allow_types;
 27     }
 28 
 29     /**
 30      * 上传文件
 31      * @access public
 32      * @param $files 等待上传的文件(表单传来的$_FILES[])
 33      * @return boolean 返回布尔值
 34      */
 35     public function upload_file($files) {
 36         $name = $files['name'];
 37         $type = $files['type'];
 38         $size = $files['size'];
 39         $tmp_name = $files['tmp_name'];
 40         $error = $files['error'];
 41 
 42         switch ($error) {
 43             case 0 : $this->errmsg = '';
 44             break;
 45             case 1 : $this->errmsg = '超过了php.ini中文件大小';
 46             break;
 47             case 2 : $this->errmsg = '超过了MAX_FILE_SIZE 选项指定的文件大小';
 48             break;
 49             case 3 : $this->errmsg = '文件只有部分被上传';
 50             break;
 51             case 4 : $this->errmsg = '没有文件被上传';
 52             break;
 53             case 5 : $this->errmsg = '上传文件大小为0';
 54             break;
 55             default : $this->errmsg = '上传文件失败!';
 56             break;
 57         }
 58         if($error == 0 && is_uploaded_file($tmp_name)) {
 59             //检测文件类型
 60             if($this->check_file_type($name) == FALSE){
 61                 return FALSE;
 62             }
 63             //检测文件大小
 64             if($size > $this->max_size){
 65                 $this->errmsg = '上传文件<font color=red>'.$name.'</font>太大,最大支持<font color=red>'.ceil($this->max_size/1024).'</font>kb的文件';
 66                 return FALSE;
 67             }
 68             $this->set_save_path();//设置文件存放路径
 69             $new_name = $this->file_name != 'date' ? $this->file_name.'.'.$this->ext : date('YmdHis').'.'.$this->ext;//设置新文件名
 70             $this->uploaded = $this->save_path.$new_name;//上传后的文件名
 71             //移动文件
 72             if(move_uploaded_file($tmp_name,$this->uploaded)){
 73                 $this->errmsg = '文件<font color=red>'.$this->uploaded.'</font>上传成功!';
 74                 return TRUE;
 75             }else{
 76                 $this->errmsg = '文件<font color=red>'.$this->uploaded.'</font>上传失败!';
 77                 return FALSE;
 78             }
 79 
 80         }
 81     }
 82 
 83     /**
 84      * 检查上传文件类型
 85      * @access public
 86      * @param string $filename 等待检查的文件名
 87      * @return 如果检查通过返回TRUE 未通过则返回FALSE和错误消息
 88      */
 89     public function check_file_type($filename){
 90         $ext = $this->get_file_type($filename);
 91         $this->ext = $ext;
 92         $allow_types = explode('|',$this->allow_types);//分割允许上传的文件扩展名为数组
 93         //echo $ext;
 94         //检查上传文件扩展名是否在请允许上传的文件扩展名中
 95         if(in_array($ext,$allow_types)){
 96             return TRUE;
 97         }else{
 98             $this->errmsg = '上传文件<font color=red>'.$filename.'</font>类型错误,只支持上传<font color=red>'.str_replace('|',',',$this->allow_types).'</font> 等文件类型!';
 99             return FALSE;
100         }
101     }
102 
103     /**
104      * 取得文件类型
105      * @access public
106      * @param string $filename 要取得文件类型的目标文件名
107      * @return string 文件类型
108      */
109     public function get_file_type($filename){
110         $info = pathinfo($filename);
111         $ext = $info['extension'];
112         return $ext;
113     }
114 
115     /**
116      * 设置文件上传后的保存路径
117      */
118     public function set_save_path(){
119         $this->save_path = (preg_match('//$/',$this->save_path)) ? $this->save_path : $this->save_path . '/';
120         if(!is_dir($this->save_path)){
121             //如果目录不存在,创建目录
122             $this->set_dir();
123         }
124     }
125 
126     /**
127      * 创建目录
128      * @access public
129      * @param string $dir 要创建目录的路径
130      * @return boolean 失败时返回错误消息和FALSE
131      */
132     public function set_dir($dir = null){
133         //检查路径是否存在
134         if(!$dir){
135             $dir = $this->save_path;
136         }
137         if(is_dir($dir)){
138             $this->errmsg = '需要创建的文件夹已经存在!';
139         }
140         $dir = explode('/', $dir);
141         foreach($dir as $v){
142             if($v){
143                 $d .= $v . '/';
144                 if(!is_dir($d)){
145                     $state = mkdir($d, 0777);
146                     if(!$state)
147                         $this->errmsg = '在创建目录<font color=red>' . $d . '时出错!';
148                 }
149             }
150         }
151         return true;
152     }
153 }

 

 

posted @ 2012-07-13 14:08  singleboss  阅读(201)  评论(0)    收藏  举报