php实现文件上传
时间:2022-05-07
本文章向大家介绍php实现文件上传,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
存档:
upload1.html(单文件上传)
1 <html>
2 <head>
3 <title>单个文件上传</title>
4 </head>
5 <body>
6 <form action="upload.php" method="post" enctype="multipart/form-data">
7 <input type="hidden" name="MAX_FILE_SIZE" value="1000000">
8 选择文件:<input type="file" name="myfile">
9 <input type="submit" value="上传文件">
10 </form>
11 </body>
12 </html>
upload2.html(多文件上传)
1 <html>
2 <head>
3 <title>多文件上传</title>
4 </head>
5 <body>
6 <form action="upload.php" method="post" enctype="multipart/form-data">
7 <input type="hidden" name="MAX_FILE_SIZE" value="1000000">
8 选择文件1:<input type="file" name="myfile[]"><br>
9 选择文件2:<input type="file" name="myfile[]"><br>
10 选择文件3:<input type="file" name="myfile[]"><br>
11 <input type="submit" value="上传文件">
12 </form>
13 </body>
14 </html>
upload.php
1 <?php
2 require "fileupload.class.php";
3 $up=new FileUpload;
4 $up ->set('path','./newpath/')
5 ->set('size',1000000)
6 ->set('allowtype',array('gif','jpg','png'))
7 ->set('israndname',false);
8 if($up->upload('myfile')){
9 print_r($up->getFileName());
10 }
11 else{
12 print_r($up->getErrorMsg());
13 }
14 ?>
fileupload.class.php
1 <?php
2 class FileUpload{
3 private $path="./uploads";
4 private $allowtype=array('jpg','gif','png');
5 private $maxsize=1000000;
6 private $israndname=true;
7 private $originName;
8 private $tmpFileName;
9 private $fileType;
10 private $fileSize;
11 private $newFileName;
12 private $errorNum=0;
13 private $errorMess="";
14
15 function set($key,$val){
16 $key=strtolower($key);
17 if(array_key_exists($key,get_class_vars(get_class($this)))){
18 $this->setOption($key,$val);
19 }
20 return $this;
21 }
22
23 function upload($fileField){
24 $return=true;
25 if(!$this->checkFilePath()){
26 $this->errorMess=$this->getError();
27 return false;
28 }
29 $name=$_FILES[$fileField]['name'];
30 $tmp_name=$_FILES[$fileField]['tmp_name'];
31 $size=$_FILES[$fileField]['size'];
32 $error=$_FILES[$fileField]['error'];
33 if(is_Array($name)){
34 $errors=array();
35 for($i=0;$i<count($name);$i++){
36 if($this->setFiles($name[$i],$tmp_name[$i],$size[$i],$error[$i])){
37 if(!$this->checkFileSize()||!$this->checkFileType()){
38 $errors[]=$this->getError();
39 $return=false;
40 }
41 }
42 else{
43 $errors[]=$this->getError();
44 $return=false;
45 }
46 if(!$return){
47 $this->setFiles();
48 }
49 }
50 if($return){
51 $fileNames=array();
52 for($i=0;$i<count($name);$i++){
53 if($this->setFiles($name[$i],$tmp_name[$i],$size[$i],$error[$i])){
54 $this->setNewFileName();
55 if(!$this->copyFile()){
56 $errors[]=$this->getError();
57 $return=false;
58 }
59 $fileNames[]=$this->newFileName;
60 }
61 }
62 $this->newFileName=$fileNames;
63 }
64 $this->errorMess=$errors;
65 return $return;
66 }
67 else{
68 if($this->setFiles($name,$tmp_name,$size,$error)){
69 if($this->checkFileSize()&&$this->checkFileType()){
70 $this->setNewFileName();
71 if($this->copyFile()){
72 return true;
73 }
74 else{
75 $return=false;
76 }
77 }
78 else{
79 $return=false;
80 }
81 }
82 else{
83 $return=false;
84 }
85 if(!$return){
86 $this->errorMess=$this->getError();
87 }
88 return $return;
89 }
90 }
91
92 public function getFileName(){
93 return $this->newFileName;
94 }
95
96 public function getErrorMsg(){
97 return $this->errorMess;
98 }
99
100 private function getError(){
101 $str="上传文件<font color='red'>{$this->originName}</font>时出错:";
102 switch($this->errorNum){
103 case 4:
104 $str.="没有文件被上传";
105 break;
106 case 3:
107 $str.="文件只有部分被上传";
108 break;
109 case 2:
110 $str.="上传的文件大小超过了HTML表单中MAX_FILE_SIZE选项指定的值";
111 break;
112 case 1:
113 $str.="上传的文件超过了php.ini中的upload_max_filesize选定限制的值";
114 break;
115 case -1:
116 $str.="未允许类型";
117 break;
118 case -2:
119 $str.="文件过大,上传的文件不能超过{$this->maxsize}个字节";
120 break;
121 case -3:
122 $str.="上传失败";
123 break;
124 case -4:
125 $str.="建立存放上传文件目录失败,请重新指定上传目录";
126 break;
127 case -5:
128 $str.="必须指定上传文件的路径";
129 break;
130 default:
131 $str.="未知错误";
132 //break;
133 }
134 return $str.'<br>';
135 }
136
137 private function setFiles($name="",$tmp_name="",$size=0,$error=0){
138 $this->setOption('errorNum',$error);
139 if($error){
140 return false;
141 }
142 $this->setOption('originName',$name);
143 $this->setOption('tmpFileName',$tmp_name);
144 $aryStr=explode(".",$name);
145 $this->setOption('fileType',strtolower($aryStr[count($aryStr)-1]));
146 $this->setOption('fileSize',$size);
147 return true;
148 }
149
150 private function setOption($key,$val){
151 $this->$key=$val;
152 }
153
154 private function setNewFileName(){
155 if($this->israndname){
156 $this->setOption('newFileName',$this->proRandName());
157 }
158 else{
159 $this->setOption('newFileName',$this->originName);
160 }
161 }
162
163 private function checkFileType(){
164 if(in_array(strtolower($this->fileType),$this->allowtype)){
165 return true;
166 }
167 else{
168 $this->setOption('errorNum',-1);
169 return false;
170 }
171 }
172
173 private function checkFileSize(){
174 if($this->fileSize>$this->maxsize){
175 $this->setOption('errorNum',-2);
176 return false;
177 }
178 else{
179 return true;
180 }
181 }
182
183 private function checkFilePath(){
184 if(empty($this->path)){
185 $this->setOption('errorNum',-5);
186 return false;
187 }
188 if(!file_exists($this->path)||!is_writable($this->path)){
189 if(!@mkdir($this->path,0755)){
190 $this->setOption('errorNum',-4);
191 return false;
192 }
193 }
194 return true;
195 }
196
197 private function proRandName(){
198 $fileName = date('YmdHis')."_".rand(100,999);
199 return $fileName.'.'.$this->fileType;
200 }
201
202 private function copyFile(){
203 if(!$this->errorNum){
204 $path=rtrim($this->path,'/').'/';
205 $path.=$this->newFileName;
206 if(@move_uploaded_file($this->tmpFileName,$path)){
207 return true;
208 }
209 else{
210 $this->setOption('errorNum',-3);
211 return false;
212 }
213 }
214 else{
215 return false;
216 }
217 }
218 }
219 ?>
结果如下:
- Ajax jsonp 跨域请求实例
- asp.net动态解析用户控件(UserControl)
- 找到多个与名为“Home”的控制器匹配的类型的解决方案
- 关于sequence问题的紧急处理(r2第26天)
- 使用动态变量进行动态数据比较(r2笔记25天)
- MVC分部视图@Html.Partial
- 基于马尔科夫链的产品评估预测
- MVC 控制器中传递dynamic(对象) 给视图
- 使用copy命令解决LONG类型的困扰(r2第24天)
- MVC 获取路由的 URL 参数值和默认值的集合。
- 用libsvm进行回归预测
- mvc路由配置.html结尾的伪静态
- 【编程基础】Java的接口和抽象类
- 循序渐进调优union相关的sql(r2笔记23天)
- php概述
- php教程
- php环境搭建
- PHP书写格式
- php变量
- php常量
- PHP注释
- php数组
- php字符串 string
- PHP整型 integer
- PHP浮点型 float
- php布尔型
- php数据类型之数组
- php数据类型之对象
- php数据类型之null
- php数据类型之间的转换
- php运算符
- php表达式
- PHP循环控制
- PHP流程控制
- php函数
- php全局变量
- PHP魔术变量
- php命名空间
- php 日期
- PHP包含文件
- php文件
- PHP 文件上传
- php Cookies
- php Sessions
- php email
- php安全email
- php错误处理
- PHP异常处理
- php过滤器
- PHP 高级过滤器
- php json
- php 表单
- PHP MySQL 简介
- PHP 连接 MySQL
- php创建数据库
- php 创建表
- php mysq 插入数据
- PHP MySQL 插入多条数据
- PHP MySQL 预处理语句
- php mysql 读取数据
- php mysql where
- PHP MySQL Order By
- PHP MySQL Update
- PHP MySQL Delete
- php ODBC