最新的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的控制语句
Oct 09 PHP
一个取得文件扩展名的函数
Oct 09 PHP
Admin generator, filters and I18n
Oct 06 PHP
解析PHP的session过期设置
Jun 29 PHP
Yii入门教程之Yii安装及hello world
Nov 25 PHP
PHP curl使用实例
Jul 02 PHP
学习PHP的数组总结【经验】
May 05 PHP
php版微信小店调用api示例代码
Nov 12 PHP
PHP递归删除多维数组中的某个值
Apr 17 PHP
PHP实现的登录页面信息提示功能示例
Jul 24 PHP
PHP封装mysqli基于面向对象的mysql数据库操作类与用法示例
Feb 25 PHP
PHP面向对象程序设计中的self、static、parent关键字用法分析
Aug 14 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
PHP 高级课程笔记 面向对象
2009/06/21 PHP
PHPMailer 中文使用说明小结
2010/01/22 PHP
PHP入门教程之PHP操作MySQL的方法分析
2016/09/11 PHP
PHP基于面向对象实现的留言本功能实例
2018/04/04 PHP
Microsfot .NET Framework4.0框架 安装失败的解决方法
2013/08/14 Javascript
js+css实现的简单易用兼容好的分页
2013/12/30 Javascript
轻松创建nodejs服务器(1):一个简单nodejs服务器例子
2014/12/18 NodeJs
详解JavaScript中的表单验证
2015/06/16 Javascript
JavaScript Math.round() 方法
2015/12/18 Javascript
学习JavaScript设计模式之单例模式
2016/01/19 Javascript
js 输入框 正则表达式(菜鸟必看教程)
2017/02/19 Javascript
12个非常有用的JavaScript技巧
2017/05/17 Javascript
浅谈es6 javascript的map数据结构
2017/12/14 Javascript
vue组件开发之用户无限添加自定义填写表单的方法
2018/08/28 Javascript
微信小程序如何调用新闻接口实现列表循环
2019/07/02 Javascript
es5 类与es6中class的区别小结
2020/11/09 Javascript
[46:04]Liquid vs VP Supermajor决赛 BO 第四场 6.10
2018/07/05 DOTA
Python中__call__用法实例
2014/08/29 Python
Python的装饰器用法学习笔记
2016/06/24 Python
分析Python读取文件时的路径问题
2018/02/11 Python
学习python分支结构
2019/05/17 Python
Pandas分组与排序的实现
2019/07/23 Python
Pycharm+django2.2+python3.6+MySQL实现简单的考试报名系统
2019/09/05 Python
Python3爬虫RedisDump的安装步骤
2021/02/20 Python
梅西酒窖:Macy’s Wine Cellar
2018/01/07 全球购物
意大利拉斐尔时尚购物网:Raffaello Network(支持中文)
2018/11/09 全球购物
护士的岗位职责
2013/12/04 职场文书
信息专业大学生自我评价分享
2014/01/17 职场文书
初中英语教学反思
2014/01/25 职场文书
退伍老兵事迹材料
2014/01/31 职场文书
劳动工资科岗位职责范本
2014/03/02 职场文书
企业金融服务方案
2014/06/03 职场文书
安全保卫工作竞聘材料
2014/08/25 职场文书
仓管员岗位职责
2015/02/03 职场文书
基于Redis zSet实现滑动窗口对短信进行防刷限流的问题
2022/02/12 Redis
mysql聚集索引、辅助索引、覆盖索引、联合索引的使用
2022/02/12 MySQL