一个经典的PHP文件上传类
2015-11-23 09:48
816 查看
需求分析如下:
(1)支持单个文件上传
(2)支持多个文件上传
(3)可以指定保存位置 可以设置上传文件允许的大小和类型 可以选择重命名和保留原名
上面为一个文件上传类 想要用到这个类需要另写一个PHP文件来调用该类
内容如下
multiFileUpload
(1)支持单个文件上传
(2)支持多个文件上传
(3)可以指定保存位置 可以设置上传文件允许的大小和类型 可以选择重命名和保留原名
<!-- 设计一个经典文件上传类 需求分析 (1)支持单个文件上传 (2)支持多个文件上传 (3)可以指定保存位置 可以设置上传文件允许的大小和类型 可以选择重命名和保留原名 程序设计 成员属性 成员方法 --> <?php error_reporting(E_ALL && !E_NOTICE); class FileUpload{ private $path = "./uploads"; private $allowtype = array('jpg','gif','png','txt'); private $maxsize = 1000000; private $israndname = true;//设置是否随机重命名文件 private $originName; private $tmpFileName; private $fileType; private $fileSize; private $newFileName; private $errorNum = 0; private $errorMess = ""; /** * 用于设置成员属性 * 可以通过连贯操作设置多个值 */ function set($key,$val){ $key = strtolower($key); if(array_key_exists($key,get_class_vars(get_class($this)))){ $this -> setOption($key,$val); } return $this; } /** * 用该函数来上传文件 */ function upload($fileField){ $return = true; //检查文件路径是否合法 if(!$this -> checkFilepath()){ $this -> errorMess = $this -> errorMess(); return false; } //将文件上传的信息取出赋值给变量 $name = $_FILES[$fileField]['name']; $tmp_name = $_FILES[$fileField]['tmp_name']; $size = $_FILES[$fileField]['size']; $error = $_FILES[$fileField]['error']; //如果是多个上传文件则$file['name']是数组 if(is_array($name)){ $errors = array(); //多个文件需要循环处理,此循环只有检查上传文件的作用,并没有真正上传 for($i = 0;$i < count($name);$i ++){ //设置文件信息 if($this -> setFiles($name[$i],$tmp_name[$i],$size[$i],$error[$i])){ if(!$this -> checkFileSize() || !$this -> checkFileType()){ $error[] = $this -> getError(); $return = false; } }else{ $errors[] = $this -> getError(); $return = false; } //如果有问题,则重新初始化属性 if(!$return) $this -> setFiles(); } if($return){ //存放所有上传文件名的变量数组 $fileNames = array(); //如果上传的多个文件都是合法的 则通过循环向服务器上传文件 for($i = 0;$i < count($name);$i ++){ if($this -> setFiles($name[$i],$tmp_name[$i],$size[$i],$error[$i])){ $this -> setNewfileName(); if(!$this -> copyFile()){ $errors[] = $this -> getError(); $return = false; } $filenames[] = $this -> newFileName; } } $this -> newFileName = $fileNames; } $this -> errorMess = $errors; return $return; }else{ //设置文件信息 if($this -> setFiles($name,$tmp_name,$size,$error)){ //上传之前先判断一下大小和类型 if($this -> checkFileSize() && $this -> checkFileType()){ //为上传文件设置新名 $this -> setNewfileName(); //上传文件 成功返回true 失败返回false if($this -> copyFile()){ return true; }else{ $return = false; } }else{ $return = false; } }else{ $return = false; } //如果$return为false 则出错 将出错信息保存在属性errorMess中 if(!$return) $this -> errorMess = $this -> getError(); return $return; } } /** * 获取上传后的文件名称 */ public function getFileName(){ return $this -> newFileName; } /** * 上传失败后,调用该方法则调用该方法返回,上传出错信息 */ public function getErrorMsg(){ return $this -> errorMess; } //设置上传错误信息 private function getError(){ $str = "上传文件<font color = 'red'>{$this -> originName}</font>时出错 :"; switch($this -> errorNum){ case 4:$str .= "没有文件被上传";break; case 3:$str .= "文件只有部分被上传";break; case 2:$str .= "上传文件的大小超过了HTML表单中指定的值";break; case 1:$str .= "上传文件的大小超过了php.ini中指定的值";break; case -1:$str .= "未允许类型";break; case -2:$str .= "文件过大,上传的文件不能超过{$this -> maxsize}个字节";break; case -3:$str .= "上传失败";break; case -4:$str .= "建立存放上传文件目录失败,请重新指定上传目录";break; case -5:$str .= "必须指定上传文件的路径";break; default:$str .= "未知错误"; } return $str.'<br>'; } //设置和$_FILES有关的内容 private function setFiles($name = "",$tmp_name = "",$size = "",$error = 0){ $this -> setOption('errorNum',$error); if($error) return false; $this -> setOption('originName',$name); $this -> setOption('tmpFileName',$tmp_name); $aryStr = explode(".",$name); $this -> setOption('fileType',strtolower($aryStr[count($aryStr) - 1])); $this -> setOption('fileSize',$size); return true; } //为单个成员属性设置值 private function setOption($key,$val){ $this -> $key = $val; } //设置上传后的文件名称 private function setNewfileName(){ if($this -> israndname){ $this -> setOption('newFileName',$this -> proRandName()); }else{ $this -> setOption('newFileName',$this -> originName); } } //检查上传文件是否是合法的类型 private function checkFileType(){ if(in_array(strtolower($this -> fileType),$this -> allowtype)){ return true; }else{ $this -> setOption('errorNum',-1); return false; } } //检查上传文件是否是允许的大小 private function checkFileSize(){ if($this -> fileSize > $this -> maxsize){ $this -> setOption('errorNum',-2); return false; }else{ return true; } } //检查是否有存放上传文件的目录 private function checkFilepath(){ if(empty($this -> path)){ $this -> setOption('errorNum',-5); return false; } if(!file_exists($this -> path) || !is_writable($this -> path)){ if(!@mkdir($this -> path)){ $this -> setOption('errorNum',-4); return false; } } return true; } //设置随机文件名 private function proRandName(){ $filename = date('YmdHis')."_".rand(100,999); return $filename.".".$this -> fileType; } //复制上传文件到指定位置 private function copyFile(){ if(!$this -> errorNum){ $path = rtrim($this -> path,'/').'/'; $path .= $this -> newFileName; if(@move_uploaded_file($this -> tmpFileName,$path)){ return true; }else{ $this -> setOption('errorNum',-3); return false; } }else{ return false; } } } ?>
上面为一个文件上传类 想要用到这个类需要另写一个PHP文件来调用该类
内容如下
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"> <html> <head><title>单个文件上传</title></head> <body> <form action = "upload.php" method = "POST" enctype = "multipart/form-data"> <input type = "hidden" name = "MAXN_FILIE_SIZE" value = "100000"> 选择文件1:<input type = "file" name = "myFile[]"> 选择文件2:<input type = "file" name = "myFile[]"> 选择文件3:<input type = "file" name = "myFile[]"> <input type = "submit" value = "上传文件"> </form> </body> </html>
multiFileUpload
相关文章推荐
- 3种php生成唯一id的方法
- Laravel中运行Gulp任务的利器 —— Laravel Elixir简介及入门教程
- php中的$_FILES
- [Windows Server 2003] IIS自带FTP安装及配置方法
- php命令行(cli)模式下报require 加载路径错误的解决方法
- PHP常用工具类大全
- Matplot使用笔记
- zabbix使用脚本发送外网邮件告警(msmtp + mutt)
- 如何设置一个严格30分钟过期的Session
- ubuntu安装php-cgi
- thinkphp3.2 留言板内容提交到数据库
- thinphp3.2 表格案例
- php调试JS输出变量
- php命令行(cli)模式下报require 加载路径错误的解决方法
- 3种php生成唯一id的方法
- php生成4位数字验证码的实现代码
- PHP如何通过AJAX方式实现登录功能
- php执行msyql脚本 sql文件
- 【转】PHP常用工具类大全
- OLAP、OLTP的介绍和比较 via csdn