把自己平常经常用的一系列的类写出来,温故而知新,期待在看过之后能对你有所帮助,并希望自己能再接再厉,能拿出更多的东西与大家共同分享:
php 代码
- <?php
- /**
- *@package Upload
- *@author 刘建武 MSN:tianhao86@hotmail.com
- *@uses 文件上传
- */
- class Upload{
- /**
- *允许上传的文件大小
- */
- public $mSize = 2097152; //2M
- /**
- * 允许的上传文件的后缀
- */
- public $mExt = "jpg|png|gif|bmp";
- /**
- * 上传路径
- */
- public $mPath ="./" ;
- /**
- * 错误信息
- */
- public $mError;
- /**
- * 设置上传路径
- */
- public function SetPath($pPath){
- return $this->mPath = $pPath;
- }
- /**
- * 设置上传大小限制
- */
- public function SetSize($pSize){
- return $this->mSize = $pSize;
- }
- /**
- * 设置上传格式
- */
- public function SetExt($pExt){
- return $this->mExt = $pExt;
- }
- /**
- * 设置错误信息
- */
- public function SetErr($pID){
- return $this->mError = $pID;
- }
- /**
- * 检测文件类型
- */
- public function CheckExt($pExt){
- $ext = explode("|", $this->mExt);
- if(in_array(strtolower($pExt), $ext)){
- return true;
- }
- else {
- return false;
- }
- }
- /**
- * 检测文件大小
- */
- public function CheckSize($pSize){
- if($this->mSize > $pSize){
- return true;
- }
- else{
- return false;
- }
- }
- /**
- * 上传文件
- */
- public function Save($pForm){
- $Form = $pForm;
- $name = $_FILES[$Form]['name'];
- $tmp = $_FILES[$Form]['tmp_name'];
- $size = $_FILES[$Form]['size'];
- $temp = explode(".", $name);
- if(!file_exists($this->mPath)){
- $this->mError = 1;
- return false;
- }
- if(!$this->CheckExt($temp[1])){
- $this->mError = 2;
- return false;
- //格式不正确
- }
- if(!$this->CheckSize($size)){
- $this->mError = 3;
- return false;
- //大小不正确
- }
- $out = time().mt_rand(0, 100);
- $file = $out.".".$temp[1];
- if(!move_uploaded_file($tmp, $this->mPath."/".$out.".".$temp[1])){
- return 4;
- }
- else {
- return $file;
- }
- }
- }
- ?>