• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php 文件上传类代码_php技巧

php 搞代码 4年前 (2022-01-26) 44次浏览 已收录 0个评论
 <BR><?php <BR>/** <BR>* 文件上传类 <BR>*/ <BR>class uploadFile { <BR>public $max_size = '1000000';//设置上传文件大小 <BR>public $file_name = 'date';//重命名方式代表以时间命名,其他则使用给予的名称 <BR>public $allow_types;//允许上传的文件扩展名,不同文件类型用“|”隔开 <BR>public $errmsg = '';//错误信息 <BR>public $uploaded = '';//上传后的文件名(包括文件路径) <BR>public $save_path;//上传文件保存路径 <BR>private $files;//提交的等待上传文件 <BR>private $file_type = array();//文件类型 <BR>private $ext = '';//上传文件扩展名 <BR>/** <BR>* 构造函数,初始化类 <BR>* @access public <BR>* @param string $file_name 上传后的文件名 <BR>* @param string $save_path 上传的目标文件夹 <BR>*/ <BR>public function __construct($save_path = './upload/',$file_name = 'date',$allow_types = '') { <BR>$this->file_name = $file_name;//重命名方式代表以时间命名,其他则使用给予的名称 <BR>$this->save_path = (preg_match('/\/$/',$save_path)) ?<i style="color:transparent">本¥文来源gaodai$ma#com搞$代*码*网(</i><strong>搞代gaodaima码</strong> $save_path : $save_path . '/'; <BR>$this->allow_types = $allow_types == '' ? 'jpg|gif|png|zip|rar' : $allow_types; <BR>} <BR>/** <BR>* 上传文件 <BR>* @access public <BR>* @param $files 等待上传的文件(表单传来的$_FILES[]) <BR>* @return boolean 返回布尔值 <BR>*/ <BR>public function upload_file($files) { <BR>$name = $files['name']; <BR>$type = $files['type']; <BR>$size = $files['size']; <BR>$tmp_name = $files['tmp_name']; <BR>$error = $files['error']; <BR>switch ($error) { <BR>case 0 : $this->errmsg = ''; <BR>break; <BR>case 1 : $this->errmsg = '超过了php.ini中文件大小'; <BR>break; <BR>case 2 : $this->errmsg = '超过了MAX_FILE_SIZE 选项指定的文件大小'; <BR>break; <BR>case 3 : $this->errmsg = '文件只有部分被上传'; <BR>break; <BR>case 4 : $this->errmsg = '没有文件被上传'; <BR>break; <BR>case 5 : $this->errmsg = '上传文件大小为0'; <BR>break; <BR>default : $this->errmsg = '上传文件失败!'; <BR>break; <BR>} <BR>if($error == 0 && is_uploaded_file($tmp_name)) { <BR>//检测文件类型 <BR>if($this->check_file_type($name) == FALSE){ <BR>return FALSE; <BR>} <BR>//检测文件大小 <BR>if($size > $this->max_size){ <BR>$this->errmsg = '上传文件<font color="red">'.$name.'</font>太大,最大支持<font color="red">'.ceil($this->max_size/1024).'</font>kb的文件'; <BR>return FALSE; <BR>} <BR>$this->set_save_path();//设置文件存放路径 <BR>$new_name = $this->file_name != 'date' ? $this->file_name.'.'.$this->ext : date('YmdHis').'.'.$this->ext;//设置新文件名 <BR>$this->uploaded = $this->save_path.$new_name;//上传后的文件名 <BR>//移动文件 <BR>if(move_uploaded_file($tmp_name,$this->uploaded)){ <BR>$this->errmsg = '文件<font color="red">'.$this->uploaded.'</font>上传成功!'; <BR>return TRUE; <BR>}else{ <BR>$this->errmsg = '文件<font color="red">'.$this->uploaded.'</font>上传失败!'; <BR>return FALSE; <BR>} <BR>} <BR>} <BR>/** <BR>* 检查上传文件类型 <BR>* @access public <BR>* @param string $filename 等待检查的文件名 <BR>* @return 如果检查通过返回TRUE 未通过则返回FALSE和错误消息 <BR>*/ <BR>public function check_file_type($filename){ <BR>$ext = $this->get_file_type($filename); <BR>$this->ext = $ext; <BR>$allow_types = explode('|',$this->allow_types);//分割允许上传的文件扩展名为数组 <BR>//echo $ext; <BR>//检查上传文件扩展名是否在请允许上传的文件扩展名中 <BR>if(in_array($ext,$allow_types)){ <BR>return TRUE; <BR>}else{ <BR>$this->errmsg = '上传文件<font color="red">'.$filename.'</font>类型错误,只支持上传<font color="red">'.str_replace('|',',',$this->allow_types).'</font>等文件类型!'; <BR>return FALSE; <BR>} <BR>} <BR>/** <BR>* 取得文件类型 <BR>* @access public <BR>* @param string $filename 要取得文件类型的目标文件名 <BR>* @return string 文件类型 <BR>*/ <BR>public function get_file_type($filename){ <BR>$info = pathinfo($filename); <BR>$ext = $info['extension']; <BR>return $ext; <BR>} <BR>/** <BR>* 设置文件上传后的保存路径 <BR>*/ <BR>public function set_save_path(){ <BR>$this->save_path = (preg_match('/\/$/',$this->save_path)) ? $this->save_path : $this->save_path . '/'; <BR>if(!is_dir($this->save_path)){ <BR>//如果目录不存在,创建目录 <BR>$this->set_dir(); <BR>} <BR>} <BR>/** <BR>* 创建目录 <BR>* @access public <BR>* @param string $dir 要创建目录的路径 <BR>* @return boolean 失败时返回错误消息和FALSE <BR>*/ <BR>public function set_dir($dir = null){ <BR>//检查路径是否存在 <BR>if(!$dir){ <BR>$dir = $this->save_path; <BR>} <BR>if(is_dir($dir)){ <BR>$this->errmsg = '需要创建的文件夹已经存在!'; <BR>} <BR>$dir = explode('/', $dir); <BR>foreach($dir as $v){ <BR>if($v){ <BR>$d .= $v . '/'; <BR>if(!is_dir($d)){ <BR>$state = mkdir($d, 0777); <BR>if(!$state) <BR>$this->errmsg = '在创建目录<font color="red">' . $d . '时出错!'; <BR>} <BR>} <BR>} <BR>return true; <BR>} <BR>} <BR>/************************************************* <BR>* 图片处理类 <BR>* <BR>* 可以对图片进行生成缩略图,打水印等操作 <BR>* 本类默认编码为UTF8 如果要在GBK下使用请将img_mark方法中打中文字符串水印iconv注释去掉 <BR>* <BR>* 由于UTF8汉字和英文字母大小(像素)不好确定,在中英文混合出现太多时可能会出现字符串偏左 <BR>* 或偏右,请根据项目环境对get_mark_xy方法中的$strc_w = strlen($this->mark_str)*7+5进 <BR>* 行调整 <BR>* 需要GD库支持,为更好使用本类推荐使用GD库2.0+ <BR>* <BR>* @author kickflip@php100 QQ263340607 <BR>*************************************************/ <BR>class uploadImg extends uploadFile { <BR>public $mark_str = 'kickflip@php100'; //水印字符串 <BR>public $str_r = 0; //字符串颜色R <BR>public $str_g = 0; //字符串颜色G <BR>public $str_b = 0; //字符串颜色B <BR>public $mark_ttf = './upload/SIMSUN.TTC'; //水印文字字体文件(包含路径) <BR>public $mark_logo = './upload/logo.png'; //水印图片 <BR>public $resize_h;//生成缩略图高 <BR>public $resize_w;//生成缩略图宽 <BR>public $source_img;//源图片文件 <BR>public $dst_path = './upload/';//缩略图文件存放目录,不填则为源图片存放目录 <BR>/** <BR>* 生成缩略图 生成后的图 <BR>* @access public <BR>* @param integer $w 缩小后图片的宽(px) <BR>* @param integer $h 缩小后图片的高(px) <BR>* @param string $source_img 源图片(路径+文件名) <BR>*/ <BR>public function img_resized($w,$h,$source_img = NULL){ <BR>$source_img = $source_img == NULL ? $this->uploaded : $source_img;//取得源文件的地址,如果为空则默认为上次上传的图片 <BR>if(!is_file($source_img)) { //检查源图片是否存在 <BR>$this->errmsg = '文件'.$source_img.'不存在'; <BR>return FALSE; <BR>} <BR>$this->source_img = $source_img; <BR>$img_info = getimagesize($source_img); <BR>$source = $this->img_create($source_img); //创建源图片 <BR>$this->resize_w = $w; <BR>$this->resize_h = $h; <BR>$thumb = imagecreatetruecolor($w,$h); <BR>imagecopyresized($thumb,$source,0,0,0,0,$w,$h,$img_info[0],$img_info[1]);//生成缩略图片 <BR>$dst_path = $this->dst_path == '' ? $this->save_path : $this->dst_path; //取得目标文件夹路径 <BR>$dst_path = (preg_match('/\/$/',$dst_path)) ? $dst_path : $dst_path . '/';//将目标文件夹后加上/ <BR>if(!is_dir($dst_path)) $this->set_dir($dst_path); //如果不存在目标文件夹则创建 <BR>$dst_name = $this->set_newname($source_img); <BR>$this->img_output($thumb,$dst_name);//输出图片 <BR>imagedestroy($source); <BR>imagedestroy($thumb); <BR>} <BR>/** <BR>*打水印 <BR>*@access public <BR>*@param string $source_img 源图片路径+文件名 <BR>*@param integer $mark_type 水印类型(1为英文字符串,2为中文字符串,3为图片logo,默认为英文字符串) <BR>*@param integer $mark_postion 水印位置(1为左下角,2为右下角,3为左上角,4为右上角,默认为右下角); <BR>*@return 打上水印的图片 <BR>*/ <BR>public function img_mark($source_img = NULL,$mark_type = 1,$mark_postion = 2) { <BR>$source_img = $source_img == NULL ? $this->uploaded : $source_img;//取得源文件的地址,如果为空则默认为上次上传的图片 <BR>if(!is_file($source_img)) { //检查源图片是否存在 <BR>$this->errmsg = '文件'.$source_img.'不存在'; <BR>return FALSE; <BR>} <BR>$this->source_img = $source_img; <BR>$img_info = getimagesize($source_img); <BR>$source = $this->img_create($source_img); //创建源图片 <BR>$mark_xy = $this->get_mark_xy($mark_postion);//取得水印位置 <BR>$mark_color = imagecolorallocate($source,$this->str_r,$this->str_g,$this->str_b); <BR>switch($mark_type) { <BR>case 1 : //加英文字符串水印 <BR>$str = $this->mark_str; <BR>imagestring($source,5,$mark_xy[0],$mark_xy[1],$str,$mark_color); <BR>$this->img_output($source,$source_img); <BR>break; <BR>case 2 : //加中文字符串水印 <BR>if(!is_file($this->mark_ttf)) { //检查字体文件是否存在 <BR>$this->errmsg = '打水印失败:字体文件'.$this->mark_ttf.'不存在!'; <BR>return FALSE; <BR>} <BR>$str = $this->mark_str; <BR>//$str = iconv('gbk','utf-8',$str);//转换字符编码 如果使用GBK编码请去掉此行注释 <BR>imagettftext($source,12,0,$mark_xy[2],$mark_xy[3],$mark_color,$this->mark_ttf,$str); <BR>$this->img_output($source,$source_img); <BR>break; <BR>case 3 : //加图片水印 <BR>if(is_file($this->mark_logo)){ //如果存在水印logo的图片则取得logo图片的基本信息,不存在则退出 <BR>$logo_info = getimagesize($this->mark_logo); <BR>}else{ <BR>$this->errmsg = '打水印失败:logo文件'.$this->mark_logo.'不存在!'; <BR>return FALSE; <BR>} <BR>$logo_info = getimagesize($this->mark_logo); <BR>if($logo_info[0]>$img_info[0] || $logo_info[1]>$img_info[1]) { //如果源图片小于logo大小则退出 <BR>$this->errmsg = '打水印失败:源图片'.$this->source_img.'比'.$this->mark_logo.'小!'; <BR>return FALSE; <BR>} <BR>$logo = $this->img_create($this->mark_logo); <BR>imagecopy ( $source, $logo, $mark_xy[4], $mark_xy[5], 0, 0, $logo_info[0], $logo_info[1]); <BR>$this->img_output($source,$source_img); <BR>break; <BR>default: //其它则为文字图片 <BR>$str = $this->mark_str; <BR>imagestring($source,5,$mark_xy[0],$mark_xy[1],$str,$mark_color); <BR>$this->img_output($source,$source_img); <BR>break; <BR>} <BR>imagedestroy($source); <BR>} <BR>/** <BR>* 取得水印位置 <BR>* @access private <BR>* @param integer $mark_postion 水印的位置(1为左下角,2为右下角,3为左上角,4为右上角,其它为右下角) <BR>* @return array $mark_xy 水印位置的坐标(索引0为英文字符串水印坐标X,索引1为英文字符串水印坐标Y, <BR>* 索引2为中文字符串水印坐标X,索引3为中文字符串水印坐标Y,索引4为水印图片坐标X,索引5为水印图片坐标Y) <BR>*/ <BR>private function get_mark_xy($mark_postion){ <BR>$img_info = getimagesize($this->source_img); <BR>$stre_w = strlen($this->mark_str)*9+5 ; //水印英文字符串的长度(px)(5号字的英文字符大小约为9px 为了美观再加5px) <BR>//(12号字的中文字符大小为12px,在utf8里一个汉字长度为3个字节一个字节4px 而一个英文字符长度一个字节大小大约为9px <BR>// 为了在中英文混合的情况下显示完全 设它的长度为字节数*7px) <BR>$strc_w = strlen($this->mark_str)*7+5 ; //水印中文字符串的长度(px) <BR>if(is_file($this->mark_logo)){ //如果存在水印logo的图片则取得logo图片的基本信息 <BR>$logo_info = getimagesize($this->mark_logo); <BR>} <BR>//由于imagestring函数和imagettftext函数中对于字符串开始位置不同所以英文和中文字符串的Y位置也有所不同 <BR>//imagestring函数是从文字的左上角为参照 imagettftext函数是从文字左下角为参照 <BR>switch($mark_postion){ <BR>case 1: //位置左下角 <BR>$mark_xy[0] = 5; //水印英文字符串坐标X <BR>$mark_xy[1] = $img_info[1]-20;//水印英文字符串坐标Y <BR>$mark_xy[2] = 5; //水印中文字符串坐标X <BR>$mark_xy[3] = $img_info[1]-5;//水印中文字符串坐标Y <BR>$mark_xy[4] = 5;//水印图片坐标X <BR>$mark_xy[5] = $img_info[1]-$logo_info[1]-5;//水印图片坐标Y <BR>break; <BR>case 2: //位置右下角 <BR>$mark_xy[0] = $img_info[0]-$stre_w; //水印英文字符串坐标X <BR>$mark_xy[1] = $img_info[1]-20;//水印英文字符串坐标Y <BR>$mark_xy[2] = $img_info[0]-$strc_w; //水印中文字符串坐标X <BR>$mark_xy[3] = $img_info[1]-5;//水印中文字符串坐标Y <BR>$mark_xy[4] = $img_info[0]-$logo_info[0]-5;//水印图片坐标X <BR>$mark_xy[5] = $img_info[1]-$logo_info[1]-5;//水印图片坐标Y <BR>break; <BR>case 3: //位置左上角 <BR>$mark_xy[0] = 5; //水印英文字符串坐标X <BR>$mark_xy[1] = 5;//水印英文字符串坐标Y <BR>$mark_xy[2] = 5; //水印中文字符串坐标X <BR>$mark_xy[3] = 15;//水印中文字符串坐标Y <BR>$mark_xy[4] = 5;//水印图片坐标X <BR>$mark_xy[5] = 5;//水印图片坐标Y <BR>break; <BR>case 4: //位置右上角 <BR>$mark_xy[0] = $img_info[0]-$stre_w; //水印英文字符串坐标X <BR>$mark_xy[1] = 5;//水印英文字符串坐标Y <BR>$mark_xy[2] = $img_info[0]-$strc_w; //水印中文字符串坐标X <BR>$mark_xy[3] = 15;//水印中文字符串坐标Y <BR>$mark_xy[4] = $img_info[0]-$logo_info[0]-5;//水印图片坐标X <BR>$mark_xy[5] = 5;//水印图片坐标Y <BR>break; <BR>default : //其它默认为右下角 <BR>$mark_xy[0] = $img_info[0]-$stre_w; //水印英文字符串坐标X <BR>$mark_xy[1] = $img_info[1]-5;//水印英文字符串坐标Y <BR>$mark_xy[2] = $img_info[0]-$strc_w; //水印中文字符串坐标X <BR>$mark_xy[3] = $img_info[1]-15;//水印中文字符串坐标Y <BR>$mark_xy[4] = $img_info[0]-$logo_info[0]-5;//水印图片坐标X <BR>$mark_xy[5] = $img_info[1]-$logo_info[1]-5;//水印图片坐标Y <BR>break; <BR>} <BR>return $mark_xy; <BR>} <BR>/** <BR>* 创建源图片 <BR>* @access private <BR>* @param string $source_img 源图片(路径+文件名) <BR>* @return img 从目标文件新建的图像 <BR>*/ <BR>private function img_create($source_img) { <BR>$info = getimagesize($source_img); <BR>switch ($info[2]){ <BR>case 1: <BR>if(!function_exists('imagecreatefromgif')){ <BR>$source = @imagecreatefromjpeg($source_img); <BR>}else{ <BR>$source = @imagecreatefromgif($source_img); <BR>} <BR>break; <BR>case 2: <BR>$source = @imagecreatefromjpeg($source_img); <BR>break; <BR>case 3: <BR>$source = @imagecreatefrompng($source_img); <BR>break; <BR>case 6: <BR>$source = @imagecreatefromwbmp($source_img); <BR>break; <BR>default: <BR>$source = FALSE; <BR>break; <BR>} <BR>return $source; <BR>} <BR>/** <BR>* 重命名图片 <BR>* @access private <BR>* @param string $source_img 源图片路径+文件名 <BR>* @return string $dst_name 重命名后的图片名(路径+文件名) <BR>*/ <BR>private function set_newname($sourse_img) { <BR>$info = pathinfo($sourse_img); <BR>$new_name = $this->resize_w.'_'.$this->resize_h.'_'.$info['basename'];//将文件名修改为:宽_高_文件名 <BR>if($this->dst_path == ''){ //如果存放缩略图路径为空则默认为源文件同文件夹 <BR>$dst_name = str_replace($info['basename'],$new_name,$sourse_img); <BR>}else{ <BR>$dst_name = $this->dst_path.$new_name; <BR>} <BR>return $dst_name; <BR>} <BR>/** <BR>* 输出图片 <BR>* @access private <BR>* @param $im 处理后的图片 <BR>* @param $dst_name 输出后的的图片名(路径+文件名) <BR>* @return 输出图片 <BR>*/ <BR>public function img_output($im,$dst_name) { <BR>$info = getimagesize($this->source_img); <BR>switch ($info[2]){ <BR>case 1: <BR>if(!function_exists('imagegif')){ <BR>imagejpeg($im,$dst_name); <BR>}else{ <BR>imagegif($im, $dst_name); <BR>} <BR>break; <BR>case 2: <BR>imagejpeg($im,$dst_name); <BR>break; <BR>case 3: <BR>imagepng($im,$dst_name); <BR>break; <BR>case 6: <BR>imagewbmp($im,$dst_name); <BR>break; <BR>} <BR>} <BR>} <BR>?> <BR>


php文件上传类源码打包


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php 文件上传类代码_php技巧
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址