php文件上传类程序代码.docx
- 文档编号:8114630
- 上传时间:2023-01-28
- 格式:DOCX
- 页数:12
- 大小:20.37KB
php文件上传类程序代码.docx
《php文件上传类程序代码.docx》由会员分享,可在线阅读,更多相关《php文件上传类程序代码.docx(12页珍藏版)》请在冰豆网上搜索。
php文件上传类程序代码
php文件上传类程序代码
我们现在只要搜索文件上传类有大把,但是真正好用的上传类不多,下面我介绍这个文件上传类是我自己使用了很久,非常不错的一个代码,大家可参考参考一。
代码如下
复制代码
php
/**
* 文件上传类
*/
classuploadFile{
public$max_size='1000000';//设置上传文件大小
public$file_name='date';//重命名方式代表以时间命名,其他则使用给予的名称
public$allow_types;//允许上传的文件扩展名,不同文件类型用“|”隔开
public$errmsg='';//错误信息
public$uploaded='';//上传后的文件名(包括文件路径)
public$save_path;//上传文件保存路径
private$files;//提交的等待上传文件
private$file_type=array();//文件类型
private$ext='';//上传文件扩展名
/**
*构造函数,初始化类
*@accesspublic
*@paramstring$file_name上传后的文件名
*@paramstring$save_path上传的目标文件夹
*/
publicfunction__construct($save_path='./upload/',$file_name='date',$allow_types=''){
$this->file_name =$file_name;//重命名方式代表以时间命名,其他则使用给予的名称
$this->save_path =(preg_match('//$/',$save_path))?
$save_path:
$save_path.'/';
$this->allow_types=$allow_types==''?
'jpg|gif|png|zip|rar':
$allow_types;
}
/**
*上传文件
*@accesspublic
*@param$files等待上传的文件(表单传来的$_FILES[])
*@returnboolean返回布尔值
*/
publicfunctionupload_file($files){
$name=$files['name'];
$type=$files['type'];
$size=$files['size'];
$tmp_name=$files['tmp_name'];
$error=$files['error'];
switch($error){
case0:
$this->errmsg='';
break;
case1:
$this->errmsg='超过了php.ini中文件大小';
break;
case2:
$this->errmsg='超过了MAX_FILE_SIZE选项指定的文件大小';
break;
case3:
$this->errmsg='文件只有部分被上传';
break;
case4:
$this->errmsg='没有文件被上传';
break;
case5:
$this->errmsg='上传文件大小为0';
break;
default:
$this->errmsg='上传文件失败!
';
break;
}
if($error==0&&is_uploaded_file($tmp_name)){
//检测文件类型
if($this->check_file_type($name)==FALSE){
returnFALSE;
}
//检测文件大小
if($size>$this->max_size){
$this->errmsg='上传文件
returnFALSE;
}
$this->set_save_path();//设置文件存放路径
$new_name=$this->file_name!
='date'?
$this->file_name.'.'.$this->ext:
date('YmdHis').'.'.$this->ext;//设置新文件名
$this->uploaded=$this->save_path.$new_name;//上传后的文件名
//移动文件
if(move_uploaded_file($tmp_name,$this->uploaded)){
$this->errmsg='文件
';
returnTRUE;
}else{
$this->errmsg='文件
';
returnFALSE;
}
}
}
/**
*检查上传文件类型
*@accesspublic
*@paramstring$filename等待检查的文件名
*@return如果检查通过返回TRUE未通过则返回FALSE和错误消息
*/
publicfunctioncheck_file_type($filename){
$ext=$this->get_file_type($filename);
$this->ext=$ext;
$allow_types= explode('|',$this->allow_types);//分割允许上传的文件扩展名为数组
//echo$ext;
//检查上传文件扩展名是否在请允许上传的文件扩展名中
if(in_array($ext,$allow_types)){
returnTRUE;
}else{
$this->errmsg='上传文件
';
returnFALSE;
}
}
/**
*取得文件类型
*@accesspublic
*@paramstring$filename要取得文件类型的目标文件名
*@returnstring文件类型
*/
publicfunctionget_file_type($filename){
$info=pathinfo($filename);
$ext=$info['extension'];
return$ext;
}
/**
*设置文件上传后的保存路径
*/
publicfunctionset_save_path(){
$this->save_path=(preg_match('//$/',$this->save_path))?
$this->save_path:
$this->save_path.'/';
if(!
is_dir($this->save_path)){
//如果目录不存在,创建目录
$this->set_dir();
}
}
/**
*创建目录
*@accesspublic
*@paramstring$dir要创建目录的路径
*@returnboolean失败时返回错误消息和FALSE
*/
publicfunctionset_dir($dir=null){
//检查路径是否存在
if(!
$dir){
$dir=$this->save_path;
}
if(is_dir($dir)){
$this->errmsg='需要创建的文件夹已经存在!
';
}
$dir=explode('/',$dir);
foreach($diras$v){
if($v){
$d.=$v.'/';
if(!
is_dir($d)){
$state=mkdir($d,0777);
if(!
$state)
$this->errmsg='在创建目录
';
}
}
}
returntrue;
}
}
/*************************************************
*图片处理类
*
*可以对图片进行生成缩略图,打水印等操作
*本类默认编码为UTF8如果要在GBK下使用请将img_mark方法中打中文字符串水印iconv注释去掉
*
*由于UTF8汉字和英文字母大小(像素)不好确定,在中英文混合出现太多时可能会出现字符串偏左
*或偏右,请根据项目环境对get_mark_xy方法中的$strc_w=strlen($this->mark_str)*7+5进
*行调整
*需要GD库支持,为更好使用本类推荐使用GD库2.0+
*
*@authorkickflip@php100QQ263340607
*************************************************/
classuploadImgextendsuploadFile{
public$mark_str='kickflip@php100'; //水印字符串
public$str_r=0;//字符串颜色R
public$str_g=0;//字符串颜色G
public$str_b=0;//字符串颜色B
public$mark_ttf='./upload/SIMSUN.TTC';//水印文字字体文件(包含路径)
public$mark_logo='./upload/logo.png'; //水印图片
public$resize_h;//生成缩略图高
public$resize_w;//生成缩略图宽
public$source_img;//源图片文件
public$dst_path='./upload/';//缩略图文件存放目录,不填则为源图片存放目录
/**
*生成缩略图生成后的图
*@accesspublic
*@paraminteger$w缩小后图片的宽(px)
*@paraminteger$h缩小后图片的高(px)
*@paramstring$source_img源图片(路径+文件名)
*/
publicfunctionimg_resized($w,$h,$source_img=NULL){
$source_img=$source_img==NULL?
$this->uploaded:
$source_img;//取得源文件的地址,如果为空则默认为上次上传的图片
if(!
is_file($source_img)){//检查源图片是否存在
$this->errmsg='文件'.$source_img.'不存在';
returnFALSE;
}
$this->source_img=$source_img;
$img_info=getimagesize($source_img);
$source=$this->img_create($source_img);//创建源图片
$this->resize_w=$w;
$this->resize_h=$h;
$thumb=imagecreatetruecolor($w,$h);
imagecopyresized($thumb,$source,0,0,0,0,$w,$h,$img_info[0],$img_info[1]);//生成缩略图片
$dst_path=$this->dst_path==''?
$this->save_path:
$this->dst_path;//取得目标文件夹路径
$dst_path=(preg_match('//$/',$dst_path))?
$dst_path:
$dst_path.'/';//将目标文件夹后加上/
if(!
is_dir($dst_path))$this->set_dir($dst_path);//如果不存在目标文件夹则创建
$dst_name=$this->set_newname($source_img);
$this->img_output($thumb,$dst_name);//输出图片
imagedestroy($source);
imagedestroy($thumb);
}
/**
*打水印
*@accesspublic
*@paramstring$source_img源图片路径+文件名
*@paraminteger$mark_type水印类型(1为英文字符串,2为中文字符串,3为图片logo,默认为英文字符串)
*@paraminteger$mark_postion水印位置(1为左下角,2为右下角,3为左上角,4为右上角,默认为右下角);
*@return打上水印的图片
*/
publicfunctionimg_mark($source_img=NULL,$mark_type=1,$mark_postion=2){
$source_img=$source_img==NULL?
$this->uploaded:
$source_img;//取得源文件的地址,如果为空则默认为上次上传的图片
if(!
is_file($source_img)){//检查源图片是否存在
$this->errmsg='文件'.$source_img.'不存在';
returnFALSE;
}
$this->source_img=$source_img;
$img_info=getimagesize($source_img);
$source=$this->img_create($source_img);//创建源图片
$mark_xy=$this->get_mark_xy($mark_postion);//取得水印位置
$mark_color=imagecolorallocate($source,$this->str_r,$this->str_g,$this->str_b);
switch($mark_type){
case1:
//加英文字符串水印
$str=$this->mark_str;
imagestring($source,5,$mark_xy[0],$mark_xy[1],$str,$mark_color);
$this->img_output($ce,$source_img);
break;
case2:
//加中文字符串水印
if(!
is_file($this->mark_ttf)){//检查字体文件是否存在
$this->errmsg='打水印失败:
字体文件'.$this->mark_ttf.'不存在!
';
returnFALSE;
}
$str=$this->mark_str;
//$str=iconv('gbk','utf-8',$str);//转换字符编码如果使用GBK编码请去掉此行注释
imagettftext($source,12,0,$mark_xy[2],$mark_xy[3],$mark_color,$this->mark_ttf,$str);
$this->img_output($source,$source_img);
break;
case3:
//加图片水印
if(is_file($this->mark_logo)){ //如果存在水印logo的图片则取得logo图片的基本信息,不存在则退出
$logo_info=getimagesize($this->mark_logo);
}else{
$this->errmsg='打水印失败:
logo文件'.$this->mark_logo.'不存在!
';
returnFALSE;
}
$logo_info=getimagesize($this->mark_logo);
if($logo_info[0]>$img_info[0]||$logo_info[1]>$img_info[1]){//如果源图片小于logo大小则退出
$this->errmsg='打水印失败:
源图片'.$this->source_img.'比'.$this->mark_logo.'小!
';
returnFALSE;
}
$logo=$this->img_create($this->mark_logo);
imagecopy($source,$logo,$mark_xy[4],$mark_xy[5],0,0,$logo_info[0],$logo_info[1]);
$this->img_output($source,$source_img);
break;
default:
//其它则为文字图片
$str=$this->mark_str;
imagestring($source,5,$mark_xy[0],$mark_xy[1],$str,$mark_color);
$this->img_output($source,$source_img);
break;
}
imagedestroy($source);
}
/**
*取得
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- php 文件 上传 程序代码
![提示](https://static.bdocx.com/images/bang_tan.gif)