PHP上传类,分享给大家,不足之处请见谅!
时间:2011-08-14
来源:互联网
不足之处请高手见谅!
PHP code
PHP code
/* * * 文件上传类 kangyangyang 2010-06-07 * $loadfile 要上传的文件的文件域 * $loadpath 文件存放路径 * $loadhold 保留文件原名 * $loadsize 文件大小限制 //默认是2048KB * $loadtype 文件类型限制 //默认支持 jpg|jpeg|gif|png|txt|rar|zip * */ class uploadfile{ private $loadfile; private $loadpath; private $loadsize; private $loadtype; private $loadhold = false; private $chkfile = true; private $file_name; private $extend_type; private $set_name; private $set_size; private $set_path; private $set_create_name; private $get_error; //构造函数 function __construct($loadfile,$loadpath,$loadhold = false,$loadsize="2097152",$loadtype="jpg|jpeg|gif|png|txt|rar|zip"){ $this->loadfile = $loadfile; $this->loadpath = $loadpath; $this->loadsize = $loadsize; $this->loadtype = $loadtype; $this->loadhold = $loadhold; $this->filecheck(); } //文件上传 function filecheck(){ $filename = $_FILES[$this->loadfile]['name']; //文件名 $filetype = $_FILES[$this->loadfile]['type']; //文件类型 $filesize = $_FILES[$this->loadfile]['size']; //文件大小 $filepath = $_FILES[$this->loadfile]['tmp_name']; //临时文件 $fileerror= $_FILES[$this->loadfile]['error']; //错误代码 if($fileerror <= 0 && $filename != ""){ if($this->loadsize >= $filesize){ $this->set_size = $filesize; $ext = explode(".",$filename); $this->set_name = $ext; $ext_type = strtolower($ext[count($ext)-1]); $this->extend_type = $ext_type; $chk_type = explode("|",$this->loadtype); if(in_array($ext_type,$chk_type)){ $f_path = $this->get_folder(); $this->set_create_name = $createname = time().rand(1,9999); $this->file_name = $createname.".".$ext_type; if($this->loadhold){ $file_path = $f_path.$filename; $this->set_path = $file_path; }else{ $file_path = $f_path.$this->file_name; $this->set_path = $file_path; } $ckfile = move_uploaded_file($filepath,$file_path); if(!$ckfile){ $this->chefile = false; } }else{ $this->chkfile = false; } }else{ $this->chkfile = false; } }else{ $this->chkfile = false; $this->get_error = $fileerror; } } //生成的文件名 function get_CreateName(){ if($this->CheckFile()){ if(!$this->loadhold){ return $this->set_create_name; } } } //返回文件夹路径 function get_folder(){ if($this->CheckFile()){ if(!file_exists($this->loadpath)){ mkdir($this->loadpath,0777,true); chmod($this->loadpath,0777); } return $this->loadpath; } } //文件扩展名 function get_type(){ if($this->CheckFile()){ return $this->extend_type; } } //文件名"不包括扩展名" function get_name(){ if($this->CheckFile()){ $filename = $this->set_name; unset($filename[count($filename)-1]); return implode(".",$filename); } } //返回文件大小 function get_size(){ if($this->CheckFile()){ return $this->set_size; } } //返回文件全路径 function get_path(){ if($this->CheckFile()){ return $this->set_path; } } //文件是否上传成功 function CheckFile(){ return $this->chkfile; } //获取错误代码 function get_error(){ if($this->CheckFile()){ return $this->get_error; } } }
作者: kangyangyangshuer 发布时间: 2011-08-14
如果上传多个文件,你打算怎么处理
作者: xuzuning 发布时间: 2011-08-14
引用 1 楼 xuzuning 的回复:
如果上传多个文件,你打算怎么处理
如果上传多个文件,你打算怎么处理
不好意思,我没有注释,我写的是单文件上传类,很久以前写的啦...
作者: kangyangyangshuer 发布时间: 2011-08-14
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28