最新的php 文件上传模型,支持多文件上传


Posted in PHP onAugust 13, 2009
<?php 
class UploadModel 
{ 
protected $keys; 
protected $err = array(); 
protected $target; 
protected $exts; 
protected $maxSize; 
protected $randName; 
protected $files = array(); 
/** 
* 初始化变量 
*/ 
public function __construct() 
{ 
$this->exts = array('jpeg','jpg','gif','png','zip','rar'); 
$this->maxSize = 1024*1024*2; 
$this->target = dirname(__FILE__) . '/upload/'; 
$this->randName = true; 
$this->keys = $this->getKeys(); 
} 
/** 
* 获取 file 的名称 
*/ 
protected function getKeys() 
{ 
$keys = array_keys($_FILES); 
return $keys; 
} 
/** 
* 设置不同类型的变量 
*/ 
public function __set($name, $value) 
{ 
$property = array('target','exts','maxSize','randName'); 
if(!in_array($name, $property)) return false; 
switch(strval($name)) 
{ 
case 'target': 
$this->$name = Configure::read('app_path') . $value; 
break; 
case 'exts': 
$this->$name = explode(',', $value); 
break; 
case 'randName': 
if($value === true || $value == 1) 
{ 
$this->$name = true; 
} 
else { 
$this->$name = false; 
} 
break; 
default: 
$this->$name = $value; 
} 
} 
/** 
* 移动上传的文件到指定的目录 
* @param $fileName 移动单个文件名称的时候,对上传的文件重新命名 
*/ 
public function save($fileName) 
{ 
$this->err = array(); 
$this->files = array(); 
if(!file_exists($this->target)) { 
mkdir($this->target); 
chmod($this->target, 0777); 
} 
foreach($this->keys as $key) 
{ 
if(is_array($_FILES[$key]['name'])) 
{ 
$count = count($_FILES[$key]['name']); 
for($i=0; $i<$count; $i++) 
{ 
$keys = array_keys($_FILES[$key]); 
foreach($keys as $item) 
{ 
$file[$item] = $_FILES[$key][$item][$i]; 
} 
$this->upload($file, $fileName); 
} 
return (count($this->err) > 0)? false:true; 
} 
else { 
return $this->upload($_FILE[$key], $fileName); 
} 
} 
} 
/** 内部处理上传文件的过程 */ 
protected function upload($file, $fileName) 
{ 
if(!is_uploaded_file($file['tmp_name'])) return false; 
if(!$this->checkExt($file)) return false; 
if(!$this->checkSize($file)) return false; 
if($this->randName) 
{ 
$newFileName = $this->target . date('YmdHis', time()) . rand(0,9) . '.' . $this->getExt($file['name']); 
} 
elseif(emptyempty($fileName)) 
{ 
$newFileName = $this->target . '/' . $file['name']; 
} 
else { 
$newFileName = $this->target . '/' . $fileName; 
} 
$result = move_uploaded_file($file['tmp_name'], $newFileName); 
if(!$result) 
{ 
return false; 
} 
else { 
$this->files[] = str_replace($this->target, $newFileName); 
return true; 
} 
} 
/** 
* 是否是可上传的文件类型 
* @param $file 文件对象 
*/ 
public function checkExt($file) 
{ 
if(!in_array($this->getExt($file['name']), $this->exts)) 
{ 
$this->err[] = $file['name'].':ext'; 
return false; 
} 
else { 
return true; 
} 
} 
/** 
* 获取文件后缀名 
*/ 
public function getExt($fileName) 
{ 
$pos = strrpos($fileName, '.')+1; 
return substr($fileName, $pos); 
} 
/** 
* 检查文件大小是否合法 
*/ 
public function checkSize($file) 
{ 
if($size > $this->maxSize) 
{ 
$this->err[] = $file['name'].':max'; 
return false; 
} 
else { 
return true; 
} 
} 
/** 
* 取得已经上传的文件名称 
*/ 
public function getFiles() 
{ 
return $this->files; 
} 
}

使用实例:
include 'uploaded.model.php'; 
$U = new UploadModel(); 
$U->target = '/tmp/'; 
$U->exts = 'jpg,gif'; 
$U->maxSize = 1024*275; //275KB 
$U->save(); 
$files = $U->getFiles(); 
print_r($files); 
include 'uploaded.model.php'; 
$U = new UploadModel(); 
$U->target = '/tmp/'; 
$U->exts = 'jpg,gif'; 
$U->maxSize = 1024*275; //275KB 
$U->save(); 
$files = $U->getFiles(); 
print_r($files);

在 MayFish 里的使用实例:
public function up() 
{ 
$U = M('SYS', 'upload'); 
$U->target = '/tmp/'; 
$U->exts = 'jpg,gif'; 
$U->maxSize = 1024*275; //275KB 
$U->save(); 
header('Location:/?a=upload'); 
} 
public function up() 
{ 
    $U = M('SYS', 'upload'); 
    $U->target = '/tmp/'; 
    $U->exts = 'jpg,gif'; 
    $U->maxSize = 1024*275; //275KB 
    $U->save(); 
    header('Location:/?a=upload'); 
}

前台代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>无标题文档</title> 
</head> 
<body> 
<form action="/?a=up" method="post" enctype="multipart/form-data"> 
<!-- 以下两上file类型控制的name属性可以任意设置,系统会自己取出input 的名称 --> 
<input name="files[]" type="file" size="30" /> 
<input name="files[]" type="file" size="30" /> 
<input type="submit" value="开始上传" /> 
</form> 
</body> 
</html>
PHP 相关文章推荐
PHP Session变量不能传送到下一页的解决方法
Nov 27 PHP
非常好用的两个PHP函数 serialize()和unserialize()
Feb 04 PHP
PHP实现的进度条效果详解
May 03 PHP
php foreach如何跳出两层循环(详解)
Nov 05 PHP
PHP模拟http请求的方法详解
Nov 09 PHP
PHP实现的字符串匹配算法示例【sunday算法】
Dec 19 PHP
php微信支付之公众号支付功能
May 30 PHP
Yii2框架自定义类统一处理url操作示例
May 25 PHP
Laravel 实现Controller向blade前台模板赋值的四种方式小结
Oct 22 PHP
php array 转json及java 转换 json数据格式操作示例
Nov 13 PHP
php多进程并发编程防止出现僵尸进程的方法分析
Feb 28 PHP
thinkphp框架实现路由重定义简化url访问地址的方法分析
Apr 04 PHP
PHP DataGrid 实现代码
Aug 12 #PHP
PHP 执行系统外部命令 system() exec() passthru()
Aug 11 #PHP
php empty函数 使用说明
Aug 10 #PHP
php 取得瑞年与平年的天数的代码
Aug 10 #PHP
php 生成WML页面方法详解
Aug 09 #PHP
彻底杜绝PHP的session cookie错误
Aug 09 #PHP
PHP 5.3.0 安装分析心得
Aug 07 #PHP
You might like
PHPLog php 程序调试追踪工具
2009/09/09 PHP
PHP5 面向对象(学习记录)
2009/12/02 PHP
推荐25款php中非常有用的类库
2014/09/29 PHP
五款PHP代码重构工具推荐
2014/10/14 PHP
js 对象是否存在判断
2009/07/15 Javascript
javascript学习笔记(五) Array 数组类型介绍
2012/06/19 Javascript
jQuery中$.fn的用法示例介绍
2013/11/05 Javascript
javascript的BOM汇总
2015/07/16 Javascript
javascript给span标签赋值的方法
2015/11/26 Javascript
VUEJS实战之修复错误并且美化时间(2)
2016/06/13 Javascript
node学习记录之搭建web服务器教程
2017/02/16 Javascript
jQuery中clone()函数实现表单中增加和减少输入项
2017/05/13 jQuery
PHP7新特性简述
2017/06/11 Javascript
JS实现DOM删除节点操作示例
2018/04/04 Javascript
Vue实现todolist删除功能
2018/06/26 Javascript
JavaScript函数apply()和call()用法与异同分析
2018/08/10 Javascript
详解Vue项目在其他电脑npm run dev运行报错的解决方法
2018/10/29 Javascript
vue中promise的使用及异步请求数据的方法
2018/11/08 Javascript
webpack file-loader和url-loader的区别
2019/01/15 Javascript
微信小程序云开发之模拟后台增删改查
2019/05/16 Javascript
Javascript和jquery在selenium的使用过程
2019/10/31 jQuery
Python实现115网盘自动下载的方法
2014/09/30 Python
Python及PyCharm下载与安装教程
2017/11/18 Python
python装饰器-限制函数调用次数的方法(10s调用一次)
2018/04/21 Python
python 字典中取值的两种方法小结
2018/08/02 Python
python实现顺时针打印矩阵
2019/03/02 Python
通过实例解析python创建进程常用方法
2020/06/19 Python
在django中实现choices字段获取对应字段值
2020/07/12 Python
python 两种方法删除空文件夹
2020/09/29 Python
俄罗斯马克西多姆家居用品网上商店:Максидом
2020/02/06 全球购物
机电工程学生自荐信范文
2013/12/07 职场文书
义和团口号
2014/06/17 职场文书
商品陈列协议书
2014/09/29 职场文书
党支部考察意见范文
2015/06/02 职场文书
2016年教师学习教师法心得体会
2016/01/20 职场文书
Windows下redis下载、redis安装及使用教程
2021/06/02 Redis