php文件上传程序

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