yjanb11

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

多文件上传处理类,可能有些地方写的不完全,可以在此基础上做出相应的修改。
引用:

<?php
include("UploadFile.php");
if ($_GET['action'] == 'save') {

$up = new upload();

$up->set_dir(dirname(__FILE__).'/upload/','{y}/{m}');
$up->set_thumb(100,80);
$up->set_watermark(dirname(__FILE__).'/jblog/images/watermark.png',6,90);
$fs = $up->execute();

//var_dump($fs);
}
?>
<html>
<head><title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body style="margin:0;padding:0">
<form name="upload" method="post" action="?action=save" enctype="multipart/form-data" style="margin:0">
<input type="file" name="attach[]" />
<input type="file" name="attach[]" />
<input type="submit" name="submit" value="上 传" />
</form>
</body>
</html>

类文件:

  1. <?php   
  2. /**  
  3.  +----------------------------------------------------------  
  4.  * franklin 文件上传处理类  
  5.  +----------------------------------------------------------  
  6.  * 文件名称  UploadFile.php  
  7.  +----------------------------------------------------------  
  8.  * 文件描述   文件上传处理类  
  9.  +----------------------------------------------------------  
  10.  * 作    者  franklin<franklin@co-insight.com>  
  11.  +----------------------------------------------------------  
  12.  * 时    间  2011-1-11  
  13.  +----------------------------------------------------------   
  14.  */  
  15. class upload {   
  16.     var $dir;             //附件存放物理目录   
  17.     var $time;             //自定义文件上传时间   
  18.     var $allow_types;     //允许上传附件类型   
  19.     var $field;             //上传控件名称   
  20.     var $maxsize;         //最大允许文件大小,单位为KB   
  21.     var $thumb_width;    //缩略图宽度   
  22.     var $thumb_height;   //缩略图高度   
  23.     var $watermark_file//水印图片地址   
  24.     var $watermark_pos;  //水印位置   
  25.     var $watermark_trans;//水印透明度   
  26.     //构造函数   
  27.     //$types : 允许上传的文件类型 , $maxsize : 允许大小 ,  $field : 上传控件名称 , $time : 自定义上传时间   
  28.     function upload($types = 'gif|jpg|png'$maxsize = 1024, $field = 'attach'$time = '') {   
  29.         $this->allow_types = explode('|',$types);   
  30.         $this->maxsize = $maxsize * 1024;   
  31.         $this->field = $field;   
  32.         $this->time = $time ? $time : time();   
  33.     }   
  34.     //设置并创建文件具体存放的目录   
  35.     //$basedir  : 基目录,必须为物理路径   
  36.     //$filedir  : 自定义子目录,可用参数{y}、{m}、{d}   
  37.     function set_dir($basedir,$filedir = '') {   
  38.         $dir = $basedir;   
  39.         !is_dir($dir) && @mkdir($dir,0777);   
  40.         if (!emptyempty($filedir)) {   
  41.             $filedir = str_replace(array('{y}','{m}','{y}'),array(date('Y',$this->time),date('m',$this->time),date('d',$this->time)),strtolower($filedir));   
  42.             $dirs = explode('/',$filedir);   
  43.             foreach ($dirs as $d) {   
  44.                 !empty($d) && $dir .= $d.'/';   
  45.                 !is_dir($dir) && @mkdir($dir,0777);   
  46.             }   
  47.         }   
  48.         $this->dir = $dir;   
  49.     }   
  50.     //图片缩略图设置,如果不生成缩略图则不用设置   
  51.     //$width : 缩略图宽度 , $height : 缩略图高度   
  52.     function set_thumb ($width = 0, $height = 0) {   
  53.         $this->thumb_width  = $width;   
  54.         $this->thumb_height = $height;   
  55.     }   
  56.     //图片水印设置,如果不生成添加水印则不用设置   
  57.     //$file : 水印图片 , $pos : 水印位置 , $trans : 水印透明度   
  58.     function set_watermark ($file$pos = 6, $trans = 80) {   
  59.         $this->watermark_file  = $file;   
  60.         $this->watermark_pos   = $pos;   
  61.         $this->watermark_trans = $trans;   
  62.     }   
  63.     /*----------------------------------------------------------------  
  64.     执行文件上传,处理完返回一个包含上传成功或失败的文件信息数组,  
  65.     其中:name 为文件名,上传成功时是上传到服务器上的文件名,上传失败则是本地的文件名  
  66.           dir  为服务器上存放该附件的物理路径,上传失败不存在该值  
  67.           size 为附件大小,上传失败不存在该值  
  68.           flag 为状态标识,1表示成功,-1表示文件类型不允许,-2表示文件大小超出  
  69.     -----------------------------------------------------------------*/  
  70.     function execute() {   
  71.         $files = array(); //成功上传的文件信息   
  72.         $field = $this->field;   
  73.         $keys = array_keys($_FILES[$field]['name']);   
  74.         foreach ($keys as $key) {   
  75.             if (!$_FILES[$field]['name'][$key]) continue;   
  76.                
  77.             $fileext = $this->fileext($_FILES[$field]['name'][$key]); //获取文件扩展名   
  78.             $filename = $this->time.mt_rand(100,999).'.'.$fileext//生成文件名   
  79.             $filedir = $this->dir;    //附件实际存放目录   
  80.             $filesize = $_FILES[$field]['size'][$key]; //文件大小   
  81.                
  82.             //文件类型不允许   
  83.             if (!in_array($fileext,$this->allow_types)) {   
  84.                 $files[$key]['name'] = $_FILES[$field]['name'][$key];   
  85.                 $files[$key]['flag'] = -1;   
  86.                 continue;   
  87.             }   
  88.             //文件大小超出   
  89.             if ($filesize > $this->maxsize) {   
  90.                 $files[$key]['name'] = $_FILES[$field]['name'][$key];   
  91.                 $files[$key]['flag'] = -2;   
  92.                 continue;   
  93.             }   
  94.             $files[$key]['name'] = $filename;   
  95.             $files[$key]['dir'] = $filedir;   
  96.             $files[$key]['size'] = $filesize;   
  97.             //保存上传文件并删除临时文件   
  98.             if (is_uploaded_file($_FILES[$field]['tmp_name'][$key])) {   
  99.                 move_uploaded_file($_FILES[$field]['tmp_name'][$key],$filedir.$filename);   
  100.                 echo $filedir.$filename;   
  101.                 @unlink($_FILES[$field]['tmp_name'][$key]);   
  102.                 $files[$key]['flag'] = 1;   
  103.                 //对图片进行加水印和生成缩略图   
  104.                 if (in_array($fileext,array('jpg','png','gif'))) {   
  105.                     if ($this->thumb_width) {   
  106.                         if ($this->create_thumb($filedir.$filename,$filedir.'thumb_'.$filename)) {   
  107.                             $files[$key]['thumb'] = 'thumb_'.$filename;  //缩略图文件名   
  108.                         }   
  109.                     }   
  110.                     $this->create_watermark($filedir.$filename);   
  111.                 }   
  112.             }   
  113.         }   
  114.         return $files;   
  115.     }   
  116.     //创建缩略图,以相同的扩展名生成缩略图   
  117.     //$aspx_file : 来源图像路径 , $thumb_file : 缩略图路径   
  118.     function create_thumb($aspx_file,$thumb_file) {   
  119.         $t_width  = $this->thumb_width;   
  120.         $t_height = $this->thumb_height;   
  121.         if (!file_exists($aspx_file)) return false;   
  122.         $aspx_info = getimagesize($aspx_file);   
  123.         //如果来源图像小于或等于缩略图则拷贝源图像作为缩略图   
  124.         if ($aspx_info[0] <= $t_width && $aspx_info[1] <= $t_height) {   
  125.             if (!copy($aspx_file,$thumb_file)) {   
  126.                 return false;   
  127.             }   
  128.             return true;   
  129.         }   
  130.         //按比例计算缩略图大小   
  131.         if ($aspx_info[0] - $t_width > $aspx_info[1] - $t_height) {   
  132.             $t_height = ($t_width / $aspx_info[0]) * $aspx_info[1];   
  133.         } else {   
  134.             $t_width = ($t_height / $aspx_info[1]) * $aspx_info[0];   
  135.         }   
  136.         //取得文件扩展名   
  137.         $fileext = $this->fileext($aspx_file);   
  138.         switch ($fileext) {   
  139.             case 'jpg' :   
  140.                 $aspx_img = imagecreatefromjpeg($aspx_file); break;   
  141.             case 'png' :   
  142.                 $aspx_img = imagecreatefrompng($aspx_file); break;   
  143.             case 'gif' :   
  144.                 $aspx_img = imagecreatefromgif($aspx_file); break;   
  145.         }   
  146.         //创建一个真彩色的缩略图像   
  147.         $thumb_img = @imagecreatetruecolor($t_width,$t_height);   
  148.         //imagecopyresampled函数拷贝的图像平滑度较好,优先考虑   
  149.         if (function_exists('imagecopyresampled')) {   
  150.             @imagecopyresampled($thumb_img,$aspx_img,0,0,0,0,$t_width,$t_height,$aspx_info[0],$aspx_info[1]);   
  151.         } else {   
  152.             @imagecopyresized($thumb_img,$aspx_img,0,0,0,0,$t_width,$t_height,$aspx_info[0],$aspx_info[1]);   
  153.         }   
  154.         //生成缩略图   
  155.         switch ($fileext) {   
  156.             case 'jpg' :   
  157.                 imagejpeg($thumb_img,$thumb_file); break;   
  158.             case 'gif' :   
  159.                 imagegif($thumb_img,$thumb_file); break;   
  160.             case 'png' :   
  161.                 imagepng($thumb_img,$thumb_file); break;   
  162.         }   
  163.         //销毁临时图像   
  164.         @imagedestroy($aspx_img);   
  165.         @imagedestroy($thumb_img);   
  166.         return true;   
  167.     }   
  168.     //为图片添加水印   
  169.     //$file : 要添加水印的文件   
  170.     function create_watermark ($file) {   
  171.         //文件不存在则返回   
  172.         if (!file_exists($this->watermark_file) || !file_exists($file)) return;   
  173.         if (!function_exists('getimagesize')) return;   
  174.            
  175.         //检查GD支持的文件类型   
  176.         $gd_allow_types = array();   
  177.         if (function_exists('imagecreatefromgif')) $gd_allow_types['image/gif'] = 'imagecreatefromgif';   
  178.         if (function_exists('imagecreatefrompng')) $gd_allow_types['image/png'] = 'imagecreatefrompng';   
  179.         if (function_exists('imagecreatefromjpeg')) $gd_allow_types['image/jpeg'] = 'imagecreatefromjpeg';   
  180.         //获取文件信息   
  181.         $fileinfo = getimagesize($file);   
  182.         $wminfo   = getimagesize($this->watermark_file);   
  183.         if ($fileinfo[0] < $wminfo[0] || $fileinfo[1] < $wminfo[1]) return;   
  184.         if (array_key_exists($fileinfo['mime'],$gd_allow_types)) {   
  185.             if (array_key_exists($wminfo['mime'],$gd_allow_types)) {   
  186.                    
  187.                 //从文件创建图像   
  188.                 $temp = $gd_allow_types[$fileinfo['mime']]($file);   
  189.                 $temp_wm = $gd_allow_types[$wminfo['mime']]($this->watermark_file);   
  190.                 //水印位置   
  191.                 switch ($this->watermark_pos) {                   
  192.                     case 1 :  //顶部居左   
  193.                         $dst_x = 0; $dst_y = 0; break;                   
  194.                     case 2 :  //顶部居中   
  195.                         $dst_x = ($fileinfo[0] - $wminfo[0]) / 2; $dst_y = 0; break;                   
  196.                     case 3 :  //顶部居右   
  197.                         $dst_x = $fileinfo[0]; $dst_y = 0; break;                   
  198.                     case 4 :  //底部居左   
  199.                         $dst_x = 0; $dst_y = $fileinfo[1]; break;                   
  200.                     case 5 :  //底部居中   
  201.                         $dst_x = ($fileinfo[0] - $wminfo[0]) / 2; $dst_y = $fileinfo[1]; break;           
  202.                     case 6 :  //底部居右   
  203.                         $dst_x = $fileinfo[0]-$wminfo[0]; $dst_y = $fileinfo[1]-$wminfo[1]; break;   
  204.                     default : //随机   
  205.                         $dst_x = mt_rand(0,$fileinfo[0]-$wminfo[0]); $dst_y = mt_rand(0,$fileinfo[1]-$wminfo[1]);   
  206.                 }   
  207.                 if (function_exists('imagealphablending')) imagealphablending($temp_wm,True); //设定图像的混色模式   
  208.                 if (function_exists('imagesavealpha')) imagesavealpha($temp_wm,True); //保存完整的 alpha 通道信息   
  209.                 //为图像添加水印   
  210.                 if (function_exists('imagecopymerge')) {   
  211.                     imagecopymerge($temp,$temp_wm,$dst_x,$dst_y,0,0,$wminfo[0],$wminfo[1],$this->watermark_trans);   
  212.                 } else {   
  213.                     imagecopymerge($temp,$temp_wm,$dst_x,$dst_y,0,0,$wminfo[0],$wminfo[1]);   
  214.                 }   
  215.                 //保存图片   
  216.                 switch ($fileinfo['mime']) {   
  217.                     case 'image/jpeg' :   
  218.                         @imagejpeg($temp,$file);   
  219.                         break;   
  220.                     case 'image/png' :   
  221.                         @imagepng($temp,$file);   
  222.                         break;   
  223.                     case 'image/gif' :    
  224.                         @imagegif($temp,$file);   
  225.                         break;   
  226.                 }   
  227.                 //销毁零时图像   
  228.                 @imagedestroy($temp);   
  229.                 @imagedestroy($temp_wm);   
  230.             }   
  231.         }   
  232.     }   
  233.     //获取文件扩展名   
  234.     function fileext($filename) {   
  235.         return strtolower(substr(strrchr($filename,'.'),1,10));   
  236.     }   
  237. }   
  238. ?>  
posted on 2011-05-15 14:36  yjanb11  阅读(117)  评论(0编辑  收藏  举报