当前位置:首页 > PHP教程 > php上传下载 > 列表

php文件上传程序

发布:smiling 来源: PHP粉丝网  添加日期:2013-12-16 17:04:21 浏览: 评论:0 

文章提供一款完整理的php文件上传程序实例代码,他可以上传图片并且把图片保存到1:按天存入目录 2:按月存入目录 ,还可以设置上传图片生成水印.

  1. <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"
  2. <html xmlns="http://www.w3.org/1999/xhtml"
  3. <head> 
  4. <meta http-equiv="content-type" content="text/html; charset=gb2312" /> 
  5. <title>上传文件程序</title> 
  6. <style type="text/css"
  7. *{ 
  8.  font-size:12px; 
  9.  margin:0; padding:0; 
  10. a:link,a:visited{ 
  11.  text-decoration:none; 
  12.  color: #393 
  13. a:hover{ 
  14.  text-decoration:underline; 
  15.  color:#0033cc 
  16. input.text{ 
  17.  border:1px solid #ccc;height:22px;line-height:22px;padding-left:5px;background:#fff;width:274px; 
  18. input.button{ 
  19.  background:#fff url(images/button.png);border:1px solid #9ea9c5;padding:2px 2px 0px 2px;margin-left:4px; margin-right:4px; 
  20. </style> 
  21. <script language=javascript> 
  22. function check()  
  23.  var strfilename=document.myform.upfile.value; 
  24.  if (strfilename==""
  25.  { 
  26.      alert("请选择要上传的文件"); 
  27.   document.myform.upfile.focus(); 
  28.      return false; 
  29.    } 
  30. </script> 
  31. </head> 
  32. <body> 
  33. <?php 
  34. /*********************** 
  35. 程序:上传文件 
  36. 功能:上传文件、缩略图、加水印 
  37. ****************************/ 
  38. include("common/upfiles.class.php"); 
  39. $path="../upload/coolsite";    //文件上传路径 
  40. $mix="smallimg";     //缩略图路径(在upload下建立) 
  41. $mark="markimg";    //加水引的图片存放路径(在upload下建立) 
  42. $text = array("www.111cn.net");   //水印内容 
  43. $ouploadnew upfiles($path,$mix,$mark);   //实例化类文件 
  44. if(isset($_post['up'])){ 
  45.   
  46.  if($_post['urlid']=='1'){ //上传图片 参数urlid 1:上传图片  2:上传其他文件.. 
  47.    
  48.   $oupload->tofile = true;  //开启则只保存缩略图或者水印图,删除原图 
  49.   $photo = $oupload->upload("upfile"); //上传的文件域 
  50.   $photourl = $oupload->fileurl."/".$photo
  51.   $newsmallimg = $oupload->smallimg($photo);  //缩略图功能 
  52.   //$newmarkimg = $oupload->watermark($photo,$text); //水印功能 
  53.   //echo $newsmallimg;  //输出缩略图路径 
  54.   //echo $newmark; //输出水印图路径 
  55.   //echo "<img src='".$newsmallimg."' border='0'>"; //输出缩略图 
  56.   //echo "<img src='".$newmark."' border='0'>"; //输出水印图 
  57.  }else
  58.   $upfilename = $oupload->upload("upfile"); //上传的文件域 
  59.  } 
  60.  $strjs = "<script language=javascript>n"
  61.  $strjs .= "parent.document.myform.upfile1.value='".$newsmallimg."'n"
  62.  $strjs .= "parent.document.myform.upfile2.value='".$photourl."'n"
  63.  $strjs .= "</script>n"
  64.  echo $strjs;  //把上次文件路径附在upfile1、upfile2中去 
  65. }else
  66. ?> 
  67. <form action="upfile.php" method="post" enctype="multipart/form-data" name="myform" onsubmit="return check()"
  68. <input type="file" name="upfile" value="" class="text"><input type="submit" name="up" value="上传" class="button"
  69. <input type="hidden" name="urlid" value="<?php echo $_get['urlid']?>"
  70. </form> 
  71. <?php }?> 
  72. </body> 
  73. </html> 
  74. <? 
  75. //upfiles.class.php 
  76. /*========================= 
  77. 上传类 upfiles.class.php 
  78. ===========================*/ 
  79. class upfiles { 
  80.   /*========================= 
  81.    //基本参数设置 
  82.   ===========================*/ 
  83.         protected $annexfolder = "upload";  //附件存放点,默认为:upload 
  84.   protected $dirtype = 2;   //1:按天存入目录 2:按月存入目录  
  85.         protected $smallfolder = "smallimg";   //缩略图存放路径,注:必须是放在 $upload下的子目录,默认为:smallimg 
  86.         protected $markfolder = "markimg";    //水印图片存放路径,注:必须是放在 $upload下的子目录,默认为:markimg 
  87.         protected $upfiletype = "jpg gif png rar zip";   //上传的类型,默认为:jpg gif png rar zip 
  88.         protected $upfilemax = 102400;   //上传大小限制,单位是"kb",默认为:1024kb 
  89.         protected $fonttype = "common/equinoxstd.otf";   //水印字体库 
  90.         protected $maxwidth = 800;   //图片最大宽度  
  91.         protected $maxheight = 600;  //图片最大高度 
  92.     
  93.   /*========================= 
  94.    //初始化上传类 
  95.   ===========================*/ 
  96.         public function __construct($annexfolder,$smallfolder,$includefolder) { 
  97.     
  98.     switch($this->dirtype) 
  99.     { 
  100.      case 1: $attach_subdir = 'day_'.date('ymd'); break
  101.      case 2: $attach_subdir = 'month_'.date('ym'); break
  102.     } 
  103.     $attach_dir = $annexfolder.'/'.$attach_subdir
  104.     $attach_dir_small = $attach_dir.'/'.$smallfolder
  105.     $attach_dir_mark = $attach_dir.'/'.$includefolder;     
  106.      
  107.     $this->rootfolder = $annexfolder
  108.                 $this->annexfolder = $attach_dir
  109.                 $this->smallfolder = $attach_dir_small
  110.     $this->markfolder = $attach_dir_mark
  111.                 //$this->fonttype = $includefolder."/nasaliza.ttf"; 
  112.         } 
  113.   public function __get($fileurl){ 
  114.    $fileurl = $this->annexfolder; 
  115.    return $fileurl
  116.   } 
  117.   /*========================= 
  118.    //上传文件 
  119.   ===========================*/ 
  120.         public function upload($inputname) { 
  121.     //检查文件夹是否存在     
  122.     if(!file_exists($this->annexfolder)){ 
  123.      if(!file_exists($this->rootfolder)) @mkdir($this->rootfolder); 
  124.      if(!file_exists($this->annexfolder)) @mkdir($this->annexfolder); 
  125.      if(!file_exists($this->smallfolder)) @mkdir($this->smallfolder); 
  126.      if(!file_exists($this->markfolder)) @mkdir($this->markfolder); 
  127.     } 
  128.     if(!file_exists($this->smallfolder)){ 
  129.      @mkdir($this->smallfolder); 
  130.     }     
  131.     if(!file_exists($this->markfolder)){ 
  132.      @mkdir($this->markfolder); 
  133.     } 
  134.          
  135.     $this->uptype = $_files[$inputname]["type"]; 
  136.     $this->upname = $_files[$inputname]["name"]; 
  137.     $this->uptmp_name = $_files[$inputname]["tmp_name"]; 
  138.     $this->upsize = $_files[$inputname]["size"]; 
  139.     $this->uperror = $_files[$inputname]["error"]; 
  140.     if($this->uptype){ 
  141.      switch ($this->uptype)///检查上传的类型 
  142.      {      
  143.       case "image/pjpeg":      
  144.        $fileextname = "jpg";      
  145.        break
  146.       case "image/jpeg":      
  147.        $fileextname = "jpg";      
  148.        break;    
  149.       case "image/gif":      
  150.        $fileextname = "gif";      
  151.        break;      
  152.       case "image/x-png":      
  153.        $fileextname = "png";      
  154.        break;      
  155.       case "application/x-shockwave-flash":      
  156.        $fileextname = "swf";      
  157.        break;      
  158.       case "text/plain":      
  159.        $fileextname = "txt";      
  160.        break;      
  161.       case "application/msword":      
  162.        $fileextname = "doc";      
  163.        break;  
  164.       case "application/vnd.ms-excel":      
  165.        $fileextname = "xls";      
  166.        break;  
  167.       case "application/x-zip-compressed":      
  168.        $fileextname = "zip";      
  169.        break;  
  170.       case "audio/mpeg"
  171.        $fileextname = "mp3";      
  172.        break;  
  173.       case "audio/x-ms-wma"
  174.        $fileextname = "wma";      
  175.        break
  176.       case "application/pdf"
  177.        $fileextname = "pdf";      
  178.        break;  
  179.       default//如果不满足上述类型,那么上传文件被判断为格式不正确!! 
  180.        //$fileextname  =strtolower(substr(strrchr(trim($this->upname), "."),1,4)); 
  181.        //$fileinfo=pathinfo($this->upname); 
  182.        //$fileextname=$fileinfo['extension']; 
  183.        $fileextname = "err"
  184.       } 
  185.      } 
  186.      if(@emptyempty($this->upname)) die("没有上传文件信息,请确认 <a href='javascript:history.go(-1);'>返回</a>"); 
  187.                  
  188.     //$name = explode(".",$this->upname);//将上传前的文件以"."分开取得文件类型 
  189.                 //$filcount = count($name);//获得截取的数量 
  190.                 //$filtype = $name[$filcount-1];//取得文件的类型 
  191.     $filtype = $fileextname
  192.                  
  193.     if(strpos($this->upfiletype,$filtype) === false){ 
  194.      die("上传文件类型仅支持 ".$this->upfiletype." 不支持 ".$filtype ." <a href='javascript:history.go(-1);'>返回</a>"); 
  195.     } 
  196.     $filsize = $this->upsize; 
  197.                 if(round($filsize/1024) > ($this->upfilemax*1024)) { 
  198.                     die("上传文件超过 ".$this->upfilemax."kb"); 
  199.                 } 
  200.                 $filename = "es_".date("ymdhis").".".$filtype;//写入数据库的文件名 
  201.                 $fileurl = $this->annexfolder."/".$filename;//上传后的文件名称 
  202.                 $upfileok = move_uploaded_file($this->uptmp_name,$fileurl); 
  203.                 if($this->uperror == 0 || $upfileok) { 
  204.                     echo "文件上传成功 <a href='javascript:history.go(-1);'>继续上传</a>";   
  205.                 } else { 
  206.                  die("上传文件失败,请确认你的上传文件不超过 $upfilemax kb 或上传时间超时"); 
  207.                 } 
  208.     return $filename
  209.     //return $fileurl; 
  210.         } 
  211.         public function getinfo($photo) { 
  212.                 $photo = $this->annexfolder."/".$photo
  213.                 $imageinfo = getimagesize($photo); 
  214.                 $imginfo["width"] = $imageinfo[0]; 
  215.                 $imginfo["height"] = $imageinfo[1]; 
  216.                 $imginfo["type"] = $imageinfo[2]; 
  217.                 $imginfo["name"] = basename($photo); 
  218.                 return $imginfo
  219.         } 
  220.   /*========================= 
  221.    //缩略图 
  222.   ===========================*/ 
  223.         public function smallimg($photo,$width=250,$height=192) { 
  224.                 $imginfo = $this->getinfo($photo); 
  225.                 $photo = $this->annexfolder."/".$photo;//获得图片源 
  226.                 $newname = substr($imginfo["name"],0,strrpos($imginfo["name"], "."))."_thumb.jpg";//新图片名称 
  227.                 if($imginfo["type"] == 1) { 
  228.                         $img = imagecreatefromgif($photo); 
  229.                 } elseif($imginfo["type"] == 2) { 
  230.                         $img = imagecreatefromjpeg($photo); 
  231.                 } elseif($imginfo["type"] == 3) { 
  232.                         $img = imagecreatefrompng($photo); 
  233.                 } else { 
  234.                         $img = ""
  235.                 } 
  236.                 if(emptyempty($img)) return false; 
  237.                 $width = ($width > $imginfo["width"]) ? $imginfo["width"] : $width;  
  238.                 $height = ($height > $imginfo["height"]) ? $imginfo["height"] : $height;  
  239.                 $srcw = $imginfo["width"];  
  240.                 $srch = $imginfo["height"];  
  241.                 if ($srcw * $width > $srch * $height) { 
  242.                         $height = round($srch * $width / $srcw); 
  243.                 } else { 
  244.                         $width = round($srcw * $height / $srch); 
  245.                 } 
  246.                 if (function_exists("imagecreatetruecolor")) { 
  247.                         $newimg = imagecreatetruecolor($width$height); 
  248.                         imagecopyresampled($newimg$img, 0, 0, 0, 0, $width$height$imginfo["width"], $imginfo["height"]); 
  249.                 } else { 
  250.                         $newimg = imagecreate($width$height); 
  251.                         imagecopyresized($newimg$img, 0, 0, 0, 0, $width$height$imginfo["width"], $imginfo["height"]); 
  252.                 } 
  253.                 if ($this->tofile) { 
  254.                         if (file_exists($this->smallfolder."/".$newname)){ 
  255.        @unlink($this->smallfolder."/".$newname); 
  256.       } 
  257.                         imagejpeg($newimg,$this->smallfolder."/".$newname); 
  258.                         return $this->smallfolder."/".$newname
  259.                 } else { 
  260.                         imagejpeg($newimg); 
  261.                 } 
  262.                 imagedestroy($newimg); 
  263.                 imagedestroy($img); 
  264.                 return $newname
  265.         } 
  266.   /*========================= 
  267.    //加水印 
  268.   ===========================*/ 
  269.         public function watermark($photo,$text) {  
  270.                 $imginfo = $this->getinfo($photo); 
  271.                 $photo = $this->annexfolder."/".$photo
  272.                 $newname = substr($imginfo["name"], 0, strrpos($imginfo["name"], ".")) . "_mark.jpg"
  273.     //$newname = substr($imginfo["name"], 0, strrpos($imginfo["name"], ".")) . ".jpg"; 
  274.                 switch ($imginfo["type"]) { 
  275.                         case 1: 
  276.                                 $img = imagecreatefromgif($photo); 
  277.                         break
  278.                         case 2: 
  279.                                 $img = imagecreatefromjpeg($photo); 
  280.                         break
  281.                         case 3: 
  282.                                 $img = imagecreatefrompng($photo); 
  283.                         break
  284.                         default
  285.                                 return false; 
  286.                 } 
  287.                 if (emptyempty($img)) return false; 
  288.                 $width = ($this->maxwidth > $imginfo["width"]) ? $imginfo["width"] : $this->maxwidth;  
  289.                 $height = ($this->maxheight > $imginfo["height"]) ? $imginfo["height"] : $this->maxheight;  
  290.                 $srcw = $imginfo["width"];  
  291.                 $srch = $imginfo["height"];  
  292.                 if ($srcw * $width > $srch * $height) { 
  293.                         $height = round($srch * $width / $srcw); 
  294.                 } else { 
  295.                         $width = round($srcw * $height / $srch); 
  296.                 } 
  297.                 if (function_exists("imagecreatetruecolor")) { 
  298.                         $newimg = imagecreatetruecolor($width$height); 
  299.                         imagecopyresampled($newimg$img, 0, 0, 0, 0, $width$height$imginfo["width"], $imginfo["height"]); 
  300.                 } else { 
  301.                         $newimg = imagecreate($width$height); 
  302.                         imagecopyresized($newimg$img, 0, 0, 0, 0, $width$height$imginfo["width"], $imginfo["height"]); 
  303.                 } 
  304.                  
  305.                 $white = imagecolorallocate($newimg, 255, 255, 255); 
  306.                 $black = imagecolorallocate($newimg, 0, 0, 0); 
  307.                 $alpha = imagecolorallocatealpha($newimg, 230, 230, 230, 80); 
  308.                 //imagefilledrectangle($newimg, 0, $height-26, $width, $height, $alpha); 
  309.                 //imagefilledrectangle($newimg, 13, $height-20, 15, $height-7, $black); 
  310.                 imagettftext($newimg, 14, 0, 20, $height-14, $white$this->fonttype, $text[0]); 
  311.                 imagettftext($newimg, 14, 0, 20, $height-6, $white$this->fonttype, $text[1]); 
  312.                 if($this->tofile) { 
  313.                         if (file_exists($this->markfolder."/".$newname)){ 
  314.        @unlink($this->markfolder."/".$newname); 
  315.       } 
  316.                         imagejpeg($newimg,$this->markfolder."/".$newname); 
  317.        
  318.       if(file_exists($this->annexfolder."/".$newname)){ 
  319.        unlink($this->annexfolder."/".$newname); 
  320.       } 
  321.        
  322.                         return $this->markfolder."/".$newname
  323.                 } else { 
  324.                         imagejpeg($newimg); 
  325.                 } 
  326.                 imagedestroy($newimg); 
  327.                 imagedestroy($img); 
  328.                 return $newname
  329.         } 
  330. }?> 

 

Tags: php 文件上传 程序

分享到: