一个经典的PHP文件上传类
需求分析如下:
(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"> <?php
/**
使用文件上传类fileupload.class.php 处理单个或多个文件上传
*/ error_reporting(E_ALL && !E_NOTICE);
require "fileupload.class.php";//将文件上传类的文件包含进来 $up = new FileUpload; //可以通过set方法设置上传的属性 设置多个属性set方法可以单独调用 也可以连贯操作一起调用多个
/* $up -> set('path','./newpath/')
-> set('size',1000000)
-> set('allowtype',array('gif','png','jpg','txt'))
-> set('israndname',false);
*/
//调用$up对象的upload方法上传文件 myfile是表单的名称 上传成功返回true 否则返回false
if($up -> upload('myFile')){
//如果是多个文件 返回数组 存放所有上传后的文件名 单文件上传则直接返回文件名称
print_r($up -> getFileName());
}else{
//如果是多个文件 返回数组 多条出错信息 单文件上传则直接返回一条错误报告
print_r($up -> getErrorMsg());
}
?>
运用问价上传类
然后在写一个HTML文件作为前端 使用上面运用到文件上传类的文件
单文件上传:
<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">
选择文件:<input type = "file" name = "myFile">
<input type = "submit" value = "上传文件">
</form>
</body>
</html>
singleFileUpload
多文件上传:
<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
一个经典的PHP文件上传类的更多相关文章
- php 文件上传类,功能相当齐全,留作开发中备用吧。
收藏一个经典好用的php 文件上传类,功能相当齐全,留作开发中备用吧. 好东西,大家都喜欢,才是真的好,哈哈!!! <?php /** * 文件上传类 */ class upload ...
- PHP 文件上传类
FileUpload.; $]; $_newname = date(,). : To ...
- php 文件上传类 实例分享
最近在研究php上传的内容,找到一个不错的php上传类,分享下. <?php /** * 文件上传类 * class: uploadFile * edit: www.jbxue.com */ c ...
- Php文件上传类class.upload.php
简介 Class.upload.php是用于管理上传文件的php文件上传类, 它可以帮助你快速的给自己的网站集成上传文件功能.不仅如此,此分类还有一些列的处理功能,可以对上传的文件或者本地的文件进行处 ...
- PHP文件上传类(页面和调用部分)
<!--upform.html内容--> <form action="upload.php" method="post" enctype=&q ...
- php多文件上传类(含示例)
在网上看到一个比较好的多文件上传类,自己改良了下,顺便用js实现了多文件浏览,php文件上传原理都是相同的,多文件上传也只是进行了循环上传而已,当然你也可以使用swfupload进行多文件上传! &l ...
- ASP.NET 文件上传类 简单好用
调用: UploadFile uf = new UploadFile(); /*可选参数*/ uf.SetIsUseOldFileName(true);//是否使用原始文件名作为新文件的文件名(默认: ...
- [上传下载] C#FileUp文件上传类 (转载)
点击下载 FileUp.zip 主要功能如下 .把上传的文件转换为字节数组 .流转化为字节数组 .上传文件根据FileUpload控件上传 .把Byte流上传到指定目录并保存为文件 看下面代码吧 // ...
- ThinkPHP文件上传类
TP框架自带文件上传类使用: 类文件在ThinkPHP/Library/Think/默认在目录下 public function upload(){ $upload = new \Think\Uplo ...
随机推荐
- windows 2013 datacenter 安装sql server2008 r2兼容性
add-windowsfeature RSAT-Clustering-AutomationServer
- ACM学习历程—Hihocoder 1290 Demo Day(动态规划)
http://hihocoder.com/problemset/problem/1290 这题是这次微软笔试的第三题,过的人比第一题少一点,这题一眼看过去就是动态规划,不过转移方程貌似不是很简单,调试 ...
- 洛谷【P1177】【模板】基数排序
题目传送门:https://www.luogu.org/problemnew/show/P1177 我对计数排序的理解:https://www.cnblogs.com/AKMer/p/9649032. ...
- Linux使用tcpdump抓取网络数据包示例
tcpdump是Linux命令行下常用的的一个抓包工具,记录一下平时常用的方式,测试机器系统是ubuntu 12.04. tcpdump的命令格式 tcpdump的参数众多,通过man tcpdump ...
- 三、使用maven创建scala工程(scala和java混一起)
本文先叙述如何配置eclipse中maven+scala的开发环境,之后,叙述如何实现spark的本地运行.最后,成功运行scala编写的spark程序. 刚开始我的eclipse+maven环境是配 ...
- vue 给嵌套的iframe子页面传数据 postMessage
Vue组件下嵌套了一个不同域下的子页面,iframe子页面不能直接获取到父页面的数据,即使数据存在localStorage中,子页面一样是获取不到的,所以只好使用postMessage传数据: < ...
- Jquery隐藏相同name的div
$("div:[name=divName]").hide(); divName(自己div的Name)
- LAMP 2.0Apache日志切割
每次访问网站就会产生若干条日志,当然前提是已经配置了日志. 配置日志的文件在 vim /usr/local/apache2/conf/extra/httpd-vhosts.conf 把注释掉的这两行打 ...
- C笔试题(一)
a和b两个整数,不用if, while, switch, for,>, <, >=, <=, ?:,求出两者的较大值. 答案: int func(int a, int b) { ...
- [原]toString()方法的复写作用, 以及打印集合.
java中的每个类的根都是Object的子类. 必然有拥有了Object的所有方法. 在package java.lang.Object源码中: public String toString() { ...