这里是来自网络朋友的一个实现的文件上传类代码,我们详细的介绍了每个变量的用处,下面看看吧,有需要可以参考一下,实例类代码如下:
-
<?php
-
-
-
-
class uploadFile {
-
-
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 = '';
-
-
-
-
-
-
-
-
public function __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;
-
}
-
-
-
-
-
-
-
-
public function upload_file($files) {
-
$name = $files['name'];
-
$type = $files['type'];
-
$size = $files['size'];
-
$tmp_name = $files['tmp_name'];
-
$error = $files['error'];
-
-
switch ($error) {
-
case 0 : $this->errmsg = '';
-
break;
-
case 1 : $this->errmsg = '超过了php.ini中文件大小';
-
break;
-
case 2 : $this->errmsg = '超过了MAX_FILE_SIZE 选项指定的文件大小';
-
break;
-
case 3 : $this->errmsg = '文件只有部分被上传';
-
break;
-
case 4 : $this->errmsg = '没有文件被上传';
-
break;
-
case 5 : $this->errmsg = '上传文件大小为0';
-
break;
-
default : $this->errmsg = '上传文件失败!';
-
break;
-
}
-
if($error == 0 && is_uploaded_file($tmp_name)) {
-
-
if($this->check_file_type($name) == FALSE){
-
return FALSE;
-
}
-
-
if($size > $this->max_size){
-
$this->errmsg = '上传文件<font color=red>'.$name.'</font>太大,最大支持<font color=red>'.ceil($this->max_size/1024).'</font>kb的文件';
-
return FALSE;
-
}
-
$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 = '文件<font color=red>'.$this->uploaded.'</font>上传成功!';
-
return TRUE;
-
}else{
-
$this->errmsg = '文件<font color=red>'.$this->uploaded.'</font>上传失败!';
-
return FALSE;
-
}
-
-
}
-
}
-
-
-
-
-
-
-
-
public function check_file_type($filename){
-
$ext = $this->get_file_type($filename);
-
$this->ext = $ext;
-
$allow_types = explode('|',$this->allow_types);
-
-
-
if(in_array($ext,$allow_types)){
-
return TRUE;
-
}else{
-
$this->errmsg = '上传文件<font color=red>'.$filename.'</font>类型错误,只支持上传<font color=red>'.str_replace('|',',',$this->allow_types).'</font>等文件类型!';
-
return FALSE;
-
}
-
}
-
-
-
-
-
-
-
-
public function get_file_type($filename){
-
$info = pathinfo($filename);
-
$ext = $info['extension'];
-
return $ext;
-
}
-
-
-
-
-
public function set_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();
-
}
-
}
-
-
-
-
-
-
-
-
-
public function set_dir($dir = null){
-
-
if(!$dir){
-
$dir = $this->save_path;
-
}
-
if(is_dir($dir)){
-
$this->errmsg = '需要创建的文件夹已经存在!';
-
}
-
$dir = explode('/', $dir);
-
foreach($dir as $v){
-
if($v){
-
$d .= $v . '/';
-
if(!is_dir($d)){
-
$state = mkdir($d, 0777);
-
if(!$state)
-
$this->errmsg = '在创建目录<font color=red>' . $d . '时出错!';
-
}
-
}
-
}
-
return true;
-
}
-
}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
class uploadImg extends uploadFile {
-
-
public $mark_str = 'kickflip@php100';
-
public $str_r = 0;
-
public $str_g = 0;
-
public $str_b = 0;
-
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/';
-
-
-
-
-
-
-
-
-
public function img_resized($w,$h,$source_img = NULL){
-
$source_img = $source_img == NULL ? $this->uploaded : $source_img;
-
if(!is_file($source_img)) {
-
$this->errmsg = '文件'.$source_img.'不存在';
-
return FALSE;
-
}
-
$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);
-
}
-
-
-
-
-
-
-
-
-
-
public function img_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.'不存在';
-
return FALSE;
-
}
-
$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) {
-
-
case 1 :
-
$str = $this->mark_str;
-
imagestring($source,5,$mark_xy[0],$mark_xy[1],$str,$mark_color);
-
$this->img_output($source,$source_img);
-
break;
-
-
case 2 :
-
if(!is_file($this->mark_ttf)) {
-
$this->errmsg = '打水印失败:字体文件'.$this->mark_ttf.'不存在!';
-
return FALSE;
-
}
-
$str = $this->mark_str;
-
-
imagettftext($source,12,0,$mark_xy[2],$mark_xy[3],$mark_color,$this->mark_ttf,$str);
-
$this->img_output($source,$source_img);
-
break;
-
-
case 3 :
-
if(is_file($this->mark_logo)){
-
$logo_info = getimagesize($this->mark_logo);
-
}else{
-
$this->errmsg = '打水印失败:logo文件'.$this->mark_logo.'不存在!';
-
return FALSE;
-
}
-
-
$logo_info = getimagesize($this->mark_logo);
-
if($logo_info[0]>$img_info[0] || $logo_info[1]>$img_info[1]) {
-
$this->errmsg = '打水印失败:源图片'.$this->source_img.'比'.$this->mark_logo.'小!';
-
return FALSE;
-
}
-
-
$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);
-
}
-
-
-
-
-
-
-
-
-
private function get_mark_xy($mark_postion){
-
$img_info = getimagesize($this->source_img);
-
-
$stre_w = strlen($this->mark_str)*9+5 ;
-
-
-
$strc_w = strlen($this->mark_str)*7+5 ;
-
-
if(is_file($this->mark_logo)){
-
$logo_info = getimagesize($this->mark_logo);
-
}
-
-
-
-
switch($mark_postion){
-
-
case 1:
-
$mark_xy[0] = 5;
-
$mark_xy[1] = $img_info[1]-20;
-
$mark_xy[2] = 5;
-
$mark_xy[3] = $img_info[1]-5;
-
$mark_xy[4] = 5;
-
$mark_xy[5] = $img_info[1]-$logo_info[1]-5;
-
break;
-
-
case 2:
-
$mark_xy[0] = $img_info[0]-$stre_w;
-
$mark_xy[1] = $img_info[1]-20;
-
$mark_xy[2] = $img_info[0]-$strc_w;
-
$mark_xy[3] = $img_info[1]-5;
-
$mark_xy[4] = $img_info[0]-$logo_info[0]-5;
-
$mark_xy[5] = $img_info[1]-$logo_info[1]-5;
-
break;
-
-
case 3:
-
$mark_xy[0] = 5;
-
$mark_xy[1] = 5;
-
$mark_xy[2] = 5;
-
$mark_xy[3] = 15;
-
$mark_xy[4] = 5;
-
$mark_xy[5] = 5;
-
break;
-
-
case 4:
-
$mark_xy[0] = $img_info[0]-$stre_w;
-
$mark_xy[1] = 5;
-
$mark_xy[2] = $img_info[0]-$strc_w;
-
$mark_xy[3] = 15;
-
$mark_xy[4] = $img_info[0]-$logo_info[0]-5;
-
$mark_xy[5] = 5;
-
break;
-
-
default :
-
$mark_xy[0] = $img_info[0]-$stre_w;
-
$mark_xy[1] = $img_info[1]-5;
-
$mark_xy[2] = $img_info[0]-$strc_w;
-
$mark_xy[3] = $img_info[1]-15;
-
$mark_xy[4] = $img_info[0]-$logo_info[0]-5;
-
$mark_xy[5] = $img_info[1]-$logo_info[1]-5;
-
break;
-
}
-
return $mark_xy;
-
}
-
-
-
-
-
-
-
-
private function img_create($source_img) {
-
$info = getimagesize($source_img);
-
switch ($info[2]){
-
case 1:
-
if(!function_exists('imagecreatefromgif')){
-
$source = @imagecreatefromjpeg($source_img);
-
}else{
-
$source = @imagecreatefromgif($source_img);
-
}
-
break;
-
case 2:
-
$source = @imagecreatefromjpeg($source_img);
-
break;
-
case 3:
-
$source = @imagecreatefrompng($source_img);
-
break;
-
case 6:
-
$source = @imagecreatefromwbmp($source_img);
-
break;
-
default:
-
$source = FALSE;
-
break;
-
}
-
return $source;
-
}
-
-
-
-
-
-
-
-
private function set_newname($sourse_img) {
-
$info = pathinfo($sourse_img);
-
$new_name = $this->resize_w.'_'.$this->resize_h.'_'.$info['basename'];
-
if($this->dst_path == ''){
-
$dst_name = str_replace($info['basename'],$new_name,$sourse_img);
-
}else{
-
$dst_name = $this->dst_path.$new_name;
-
}
-
return $dst_name;
-
}
-
-
-
-
-
-
-
-
-
public function img_output($im,$dst_name) {
-
$info = getimagesize($this->source_img);
-
switch ($info[2]){
-
case 1:
-
if(!function_exists('imagegif')){
-
imagejpeg($im,$dst_name);
-
}else{
-
imagegif($im, $dst_name);
-
}
-
break;
-
case 2:
-
imagejpeg($im,$dst_name);
-
break;
-
case 3:
-
imagepng($im,$dst_name);
-
break;
-
case 6:
-
imagewbmp($im,$dst_name);
-
break;
-
}
-
}
-
-
}
-
?>
这个写成了上传文件类就方便多了,把上面代码保存一个文件它就可以公共调用与修改删除了.
(责任编辑:最模板) |