|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<html><head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>title</title></head><body><!-- multipart/form-data 表示有文件上传 --><form action="doAction_1.php"method="post" enctype="multipart/form-data">请选择你要上传的文件: <input type="file" name="file"> <br> <input type="submit" name="提交"></form></body></html> |
php上传前台
|
1
|
<!-- multipart/form-data 表示有文件上传 --> |
后台使用 $_FILES接收的格式
|
1
2
3
4
5
6
7
8
9
10
11
12
|
Array( [file] => Array ( [name] => 文件的名字 [type] => 文件的种类 [tmp_name] => 文件的临时存储地点 [error] => 错误号 [size] => 文件的大小 )) |
这里的error有 8中错误
// UPLOAD_ERR_OK Value: 0
当 error = 0 没有错误发生 文件上传成功
// UPLOAD_ERR_INI_SIZE Value: 1
当 error = 1 上传的文件超过了 php.ini 中 upload_max_filesize 的大小
// UPLOAD_ERR_FORM_SIZE Value: 2
当 error = 2 上传文件的大小超过了 html 表单中 max_file_size的大小
// UPLOAD_ERR_PARTIAL Value: 3
当 error = 3 文件只有部分被上传
// UPLOAD_ERR_NO_FILE Value: 4
当 error =4 没有文件被上传
// UPLOAD_ERR_NO_TMP_DIR Value: 6
当 error = 6 找不到临时文件
// UPLOAD_ERR_CANT_WRITE Value: 7
当 error = 7 临时文件写入失败
// UPLOAD_ERR_EXTENSION Value: 8
当 error =8 php 扩展程序被中断
后台代码
1,接收前台传过来的 doAction_1.php
|
1
2
3
4
5
6
7
8
9
|
<?phpheader(‘content-type:text/html;charset=utf-8‘);//设置为UTF-8显示防止乱码 require_once ‘upload.class.php‘; $upload = new upload(‘file‘);//创建对象 $upload->uploadFile(); //$upload = new $upload();?> |
使用面向对象的方法调用
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
<?phpclass upload{ protected $maxSize; //规定最大的上传大小 protected $fileName; // 传过来的文件名称 protected $fileInfo; // protected $name; //传过来的文件类型 protected $all; //所有的文件类型 protected $path; //构造函数 public function __construct($fileName=‘file‘,$path=‘uploads‘,$maxSize=‘999999‘,$all=array(‘txt‘,‘png‘,‘doc‘,‘jpg‘)){ //语句前面加上@ 可以去掉出错的语句 $this->fileName=$fileName; $this->maxSize= $maxSize; $this->all =$all; $this->path=$path; $this->fileInfo=$_FILES[$fileName]; } //检查是否是HTTPPOST提交 protected function checkHttpPost(){ if(!is_uploaded_file($this->fileInfo[‘tmp_name‘])){ exit("文件不是通过HTTP POST传过来的"); return false; } return ture; } //检查是否报错 public function checkError(){ //$i=1; switch($this->fileInfo[‘error‘]){ case 1: $this->error= ‘当 error = 1 上传的文件超过了 php.ini 中 upload_max_filesize 的大小‘; //echo 123; break; case 2: $this->error= "当 error = 2 上传文件的大小超过了 html 表单中 max_file_size的大小"; break; case 3: $this->error= "当 error = 3 文件只有部分被上传"; break; case 4: $this->error=‘当 error =4 没有文件被上传‘; break; case 6: $this->error="当 error = 6 找不到临时文件"; break; case 7: $this->error ="系统错误"; case 8: $this->error ="系统错误"; } } //检查是否超过规定大小 protected function checkSize(){ if($this->fileInfo[‘size‘]>$this->maxSize){ echo "checksize"; //$this->error=‘上传文件过大‘; return false; } return true; } //检查文件类型 protected function checkKind(){ $this->name= end(explode ( "." , $this->fileInfo[‘name‘])); if(!in_array($this->name,$this->all)){ echo "checksize"; exit(‘非法的文件类型‘); return false; } return true; } public function uploadFile(){ move_uploaded_file($this->fileInfo[‘tmp_name‘],$this->path.‘/‘.iconv("UTF-8","gb2312",$this->fileInfo[‘name‘])); if($this->checkKind()&&$this->checkSize()){ echo "132"; //&&&&$this->checkHttpPost()&&$this->checkSize() } } public function error(){ exit(‘<h1 >‘.$this->error.‘</h1>‘); } } ?> |
原文:http://www.cnblogs.com/gewenrui/p/4888862.html