'./mytest/'));
* $up->uploadfile('userfile'); //userfile 为input框的name值 多文件时input 的name值要有中括号: name=userfile[] 没有中括号只能上传第一个文件
* echo $up->geterrormsg(); //得到错误信息
*
* 基本功能有 都以参数形式传入构造函数
* 指定上传文件格式 [array] allowtype
* 指定文件大小 [int] maxsize
*
* 实例1 上传头像到指定目录 并以随即名保存 文件名长度为20(默认为20)
* $up = new fileupload(array('savepath'=>'./avatar/', 'israndname'=>true, 'newfilenamelength'=>20));
* 实例2 上传头像到指定目录 并且用用户id作为文件名 如上传 photo.png 保存为 2.png
* $up = new fileupload(array('savepath'=>'./avatar/', 'israndname'=>false, 'givenfilename'=>2));
* 实例3 上传头像到指定目录 在目录下以日期为单位建立子目录保存头像
* $up = new fileupload(array('savepath'=>'./avatar/', 'subdirpattern'=>'y/m' , 'israndname'=>false, 'givenfilename'=>2));
* 以上生成目录为 './avatar/2013/10'
*/
class fileupload {
private $name = 'name';
private $type = 'type';
private $tmp_name = 'tmp_name';
private $error = 'error';
private $size = 'size';
// 构造方法用到的字段
private $savepath = ''; //指定上传文件保存的路径
private $subdirpattern = ''; // 结合savepath使用 为空表示不添加子目录,不为空 比如 'y/m' 表示 2011/01 那么保存路径就是 $savepath . '/2011/01'
private $allowtype = array('gif', 'jpg', 'png');
private $maxsize = 204800; //byte 200k
private $israndname = true; //是否随机重命名 true false不随机 使用原文件名
private $givenfilename = ''; //使用给定的文件名 配合 israndname 使用
private $ignoreemptyfile = true; //是否忽略没有上传文件的文本域
private $newfilenamelength = 20;
// 本类用到的字段
private $errormessage = '';
private $uploadfilearray = null;
private $originalfilename = '';
private $expandedname = '';
public $newfilename = array();
//1. 指定上传路径, 2,充许的类型, 3,限制大小, 4,是否使用随机文件名称
//new fileupload( array('savepath'=>'./uploads/', 'allowtype'=>array('txt','gif'), 'israndname'=>true) );
public function __construct($args=array()) {
foreach($args as $key => $value) {
$key = strtolower($key);
//查看用户参数中数组的下标是否和成员属性名相同
//if(!in_array( $key, get_class_vars(get_class($this)) )) {
// continue;
//}
$this->setargs($key, $value);
}
}
private function setargs($key, $value) {
$this->$key = $value;
}
/**
* 得到文件大小
* @param int size 字节数
*/
private function getfilesize($size) {
$unit = 'bytes';
if($size return $size.$unit;
} else if($size $unit = 'kb';
$size = round($size / 1024, 2);
} else if($size $unit = 'mb';
$size = round($size / pow(1024, 2), 2);
} else if($size $unit = 'gb';
$size = round($size / pow(1024, 3), 2);
} else {
$unit = 'tb';
$size = round($size / pow(1024, 4), 2);
}
return $size.$unit;
}
/**
* 得到一个数组的键组成的数组
*/
private function myarray_keys(& $arr) {
$returnarr = array();
foreach($arr as $key => $val) {
$returnarr[] = $key;
}
return $returnarr;
}
/* 没排序时
array
(
[f1] => array
(
[name] => array
(
[0] => winter.jpg
)
// 凡是加了[] 这里的name格式就成了数组 这是才能支持多文件
// 凡是不加[] 这里的name格式就是字符串 也就是只能上传第一个文件
[type] => array
(
[0] => image/jpeg
)
[tmp_name] => array
(
[0] => c:\windows\temp\php38d.tmp
)
[error] => array
(
[0] => 0
)
[size] => array
(
[0] => 105542
)
)
)
*/
/**
* 重新排列上传文件
* $uploadfilearray 上传的文件的数组
*/
private function resortfile(& $uploadfilearray) {
// input name没有[] 时是字符串
// 有[] 时是数组
$multiflag = is_array($uploadfilearray[$this->name]) ? true : false;
$file_arr = array();
$file_num = $multiflag ? count($uploadfilearray[$this->name]) : 1; //计算上传的文件数
$file_keys = $this->myarray_keys($uploadfilearray); //得到数组,包含了name type error tmp_name size
for($i=0; $i foreach($file_keys as $value) {
$file_arr[$i][$value] = $multiflag ? $uploadfilearray[$value][$i] : $uploadfilearray[$value];
}
}
return $file_arr;
}
/**
* 错误报告
* $errorno 错误号
*/
private function seterrormsg($errorno){
$msg = 上传文件 {$this->originalfilename} 时出错: ;
switch($errorno){
case 1:
case 2:
$msg .= '文件过大,无法上传'; //配置文件中的大小
case 3:
$msg .= '文件只被部分上传'; break;
case -1:
$msg .= '不充许的文件类型'; break;
case -2:
$msg .= '文件过大,上传文件不能超过'.$this->getfilesize($this->maxsize); break;
case -3:
$msg .= '上传失败'; break;
case -4:
$msg .= '建立存放上传文件目录失败,请重新指定上传目录'; break;
case -5:
$msg .= '必须指定上传文件的路径'; break;
case -6:
$msg .= '不是上传的文件'; break;
default: $msg .= 未知错误;
}
return $msg.'
';
}
/**
* 检查有没有指定上传路径
*/
private function emptysavepath() {
if(empty($this->savepath)) {
$this->errormessage .= $this->seterrormsg(-5);
return true;
}
return false;
}
/**
* 得到扩展名
* $filename 文件名
*/
private function getexpandedname() {
$pos = strrpos($this->originalfilename, '.');
return substr($this->originalfilename, $pos+1);
}
/**
* 检查文件扩展名是够合法
*/
private function islegalexpandedname() {
if(in_array($this->expandedname, $this->allowtype)) {
return true;
}
$this->errormessage .= $this->seterrormsg(-1);
return false;
}
/**
* 检查上传的文件有没有错误
* $i 第几个文件
*/
private function haserror($i) {
$errorno = $this->uploadfilearray[$i][$this->error];
if(0 == $errorno) {
return 0; //文件正常
} else if(4 == $errorno) {
return 4; //没有上传文件
}
$this->errormessage .= $this->seterrormsg($errorno);
return 99; //文件有错误
}
/**
* 检查文件大小
* $i 第几个文件
*/
private function islegalsize($i) {
$filesize = $this->uploadfilearray[$i][$this->size];
if($filesize maxsize) {
return true;
}
$this->errormessage .= $this->seterrormsg(-2);
return false;
}
/**
* 检查所给出的文件是否是通过http post上传的
* $i 第几个文件
*/
private function isuploadedfile($i) {
$tmpname = $this->uploadfilearray[$i][$this->tmp_name];
if(is_uploaded_file($tmpname)) {
return true;
}
$this->errormessage .= $this->seterrormsg(-6);
return false;
}
/**
* 得到新文件名(如果用户指定不用新文件名则使用旧文件名)
*
*/
private function initnewfilename() {
$str = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz';
$chars = $this->originalfilename; // 有后缀 .jpg
if($this->israndname) {
$chars = '';
for($i=0; $inewfilenamelength-8; $i++) {
$chars .= substr($str, mt_rand(0, strlen($str)-1), 1);
}
$chars .= date('ymd', time());
$chars = $chars . '.' . $this->expandedname;
} else {
// 给定了使用指定的名字
if('' != $this->givenfilename) {
$chars = $this->givenfilename . '.' . $this->expandedname;
}
}
return $chars;
}
/**
* 复制文件到指定地方
* $i 第几个文件
*/
private function copyfile($i) {
$this->newfilename[$i] = $this->initnewfilename();
if(is_dir($this->savepath)) {
@move_uploaded_file($this->uploadfilearray[$i][$this->tmp_name], $this->savepath.$this->newfilename[$i]);
} else {
die('上传目录创建失败');
}
}
/**
* 检查是否有空文件
*/
private function chkemptyfile(& $arr) {
$flag = 1;
for($i = 0; $i if(intval($arr[$i][$this->error]) == 4) {
$flag = 4;
break;
}
}
return $flag;
}
/**
* 初始化上传文件夹
*/
private function initsavepath() {
$this->savepath = rtrim($this->savepath, '/') . '/';
!empty($this->subdirpattern) && $this->savepath = $this->savepath . date($this->subdirpattern, time()) . '/';
$tmpsavepath = rtrim($this->savepath, '/');
if(!is_dir($tmpsavepath)) {
$dirs = explode('/', $tmpsavepath);
$realdir = '';
for($i = 0; $i if($dirs[$i] == '') continue;
if($dirs[$i] == '.') {
$realdir .= './';
} else {
$realdir = $realdir . $dirs[$i] . '/';
}
if(!is_dir($realdir))
mkdir($realdir);
}
}
}
/**
* 开始上传文件方法
*/
private function startupload() {
for($i=0; $iuploadfilearray); $i++) {
if(0 === $this->haserror($i)) {
$this->originalfilename = $this->uploadfilearray[$i][$this->name]; // aa.jpg
$this->expandedname = $this->getexpandedname();
if($this->islegalexpandedname()) {
if($this->islegalsize($i)) {
//if($this->isuploadedfile($i)) {
$this->copyfile($i);
//}
}
}
}
}
}
/**
* 上传文件 入口
* $filefield input框的name属性值
*/
public function uploadfile($filefield) {
//检查上传路径
if(true === $this->emptysavepath()) {
return false;
}
if(0 !== count($_files)) {
//重新排列上传文件
$this->uploadfilearray = $this->resortfile($_files[$filefield]);
//开始上传文件
if( !$this->ignoreemptyfile && 4 == $this->chkemptyfile($this->uploadfilearray) ) {
die('强制全部上传模式');
} else {
$this->initsavepath(); // 初始化上传路径
$this->startupload();
}
}
}
/**
* de到错误信息
*/
public function geterrormsg() {
return $this->errormessage;
}
public function getuploadfilename() {
foreach($this->newfilename as $key=>$val) {
$this->newfilename[$key] = $this->savepath . $this->newfilename[$key];
}
return $this->newfilename;
}
}
?>
fileupload.rar ( 3.89 kb 下载:205 次 )
ad:真正免费,域名+虚机+企业邮箱=0元