最模板 - 外贸网站建设,外贸网站模板

最模板

当前位置: 首页 > 建站教程 > php教程 >

php文件上传程序

时间:2014-06-09 16:40来源: 作者: 点击: 次
文章提供一款完整理的php文件上传程序实例代码,他可以上传图片并且把图片保存到1:按天存入目录 2:按月存入目录 ,还可以设置上传图片生成水印. !doctypehtml public -//w3c//dtdxhtml1.0transitional//en

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

 

(责任编辑:admin)
------分隔线----------------------------
栏目列表
推荐内容