PHP图片上传程序,完整版

从PHP100上搜刮来的,功能很强大。几乎考虑到了每个细节,与大家分享!~~~

[php]view plaincopy

  1. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  2. <?php
  3. /******************************************************************************
  4. 参数说明:
  5. $max_file_size : 上传文件大小限制, 单位BYTE
  6. $destination_folder : 上传文件路径
  7. $watermark : 是否附加水印(1为加水印,其他为不加水印);
  8. 使用说明:
  9. 1. 将PHP.INI文件里面的"extension=php_gd2.dll"一行前面的;号去掉,因为我们要用到GD库;
  10. 2. 将extension_dir =改为你的php_gd2.dll所在目录;
  11. ******************************************************************************/
  12. //上传文件类型列表
  13. $uptypes=array(
  14. 'image/jpg',
  15. 'image/jpeg',
  16. 'image/png',
  17. 'image/pjpeg',
  18. 'image/gif',
  19. 'image/bmp',
  20. 'image/x-png'
  21. );
  22. $max_file_size=2000000; //上传文件大小限制, 单位BYTE
  23. $destination_folder="uploadimg/"; //上传文件路径
  24. $watermark=1; //是否附加水印(1为加水印,其他为不加水印);
  25. $watertype=1; //水印类型(1为文字,2为图片)
  26. $waterposition=1; //水印位置(1为左下角,2为右下角,3为左上角,4为右上角,5为居中);
  27. $waterstring="http://www.xplore.cn/"; //水印字符串
  28. $waterimg="xplore.gif"; //水印图片
  29. $imgpreview=1; //是否生成预览图(1为生成,其他为不生成);
  30. $imgpreviewsize=1/2; //缩略图比例
  31. ?>
  32. <html>
  33. <head>
  34. <title>ZwelL图片上传程序</title>
  35. <style type="text/css">
  36. <!--
  37. body
  38. {
  39. font-size: 9pt;
  40. }
  41. input
  42. {
  43. background-color: #66CCFF;
  44. border: 1px inset #CCCCCC;
  45. }
  46. -->
  47. </style>
  48. </head>
  49. <body>
  50. <form enctype="multipart/form-data" method="post" name="upform">
  51. 上传文件:
  52. <input name="upfile" type="file">
  53. <input type="submit" value="上传"><br>
  54. 允许上传的文件类型为:<?=implode(', ',$uptypes)?>
  55. </form>
  56. <?php
  57. if ($_SERVER['REQUEST_METHOD'] == 'POST')
  58. {
  59. if (!is_uploaded_file($_FILES["upfile"][tmp_name])) //这里的tmp_name找不到,看看屏蔽
  60. //是否存在文件
  61. {
  62. echo "图片不存在!";
  63. exit;
  64. }
  65. $file = $_FILES["upfile"];
  66. if($max_file_size < $file["size"])
  67. //检查文件大小
  68. {
  69. echo "文件太大!";
  70. exit;
  71. }
  72. if(!in_array($file["type"], $uptypes))
  73. //检查文件类型
  74. {
  75. echo "文件类型不符!".$file["type"];
  76. exit;
  77. }
  78. if(!file_exists($destination_folder))
  79. {
  80. mkdir($destination_folder);
  81. }
  82. $filename=$file["tmp_name"];
  83. $image_size = getimagesize($filename);
  84. $pinfo=pathinfo($file["name"]);
  85. $ftype=$pinfo['extension'];
  86. $destination = $destination_folder.time().".".$ftype;
  87. if (file_exists($destination) && $overwrite != true)
  88. {
  89. echo "同名文件已经存在了";
  90. exit;
  91. }
  92. if(!move_uploaded_file ($filename, $destination))
  93. {
  94. echo "移动文件出错";
  95. exit;
  96. }
  97. $pinfo=pathinfo($destination);
  98. $fname=$pinfo[basename]; //这里应改成$fname=$pinfo["basename"]
  99. echo " <font color=red>已经成功上传</font><br>文件名: <font color=blue>".$destination_folder.$fname."</font><br>";
  100. echo " 宽度:".$image_size[0];
  101. echo " 长度:".$image_size[1];
  102. echo "<br> 大小:".$file["size"]." bytes";
  103. if($watermark==1)
  104. {
  105. $iinfo=getimagesize($destination,$iinfo);
  106. $nimage=imagecreatetruecolor($image_size[0],$image_size[1]);
  107. $white=imagecolorallocate($nimage,255,255,255);
  108. $black=imagecolorallocate($nimage,0,0,0);
  109. $red=imagecolorallocate($nimage,255,0,0);
  110. imagefill($nimage,0,0,$white);
  111. switch ($iinfo[2])
  112. {
  113. case 1:
  114. $simage =imagecreatefromgif($destination);
  115. break;
  116. case 2:
  117. $simage =imagecreatefromjpeg($destination);
  118. break;
  119. case 3:
  120. $simage =imagecreatefrompng($destination);
  121. break;
  122. case 6:
  123. $simage =imagecreatefromwbmp($destination);
  124. break;
  125. default:
  126. die("不支持的文件类型");
  127. exit;
  128. }
  129. imagecopy($nimage,$simage,0,0,0,0,$image_size[0],$image_size[1]);
  130. imagefilledrectangle($nimage,1,$image_size[1]-15,80,$image_size[1],$white);
  131. switch($watertype)
  132. {
  133. case 1: //加水印字符串
  134. imagestring($nimage,2,3,$image_size[1]-15,$waterstring,$black);
  135. break;
  136. case 2: //加水印图片
  137. $simage1 =imagecreatefromgif("xplore.gif");
  138. imagecopy($nimage,$simage1,0,0,0,0,85,15);
  139. imagedestroy($simage1);
  140. break;
  141. }
  142. switch ($iinfo[2])
  143. {
  144. case 1:
  145. //imagegif($nimage, $destination);
  146. imagejpeg($nimage, $destination);
  147. break;
  148. case 2:
  149. imagejpeg($nimage, $destination);
  150. break;
  151. case 3:
  152. imagepng($nimage, $destination);
  153. break;
  154. case 6:
  155. imagewbmp($nimage, $destination);
  156. //imagejpeg($nimage, $destination);
  157. break;
  158. }
  159. //覆盖原上传文件
  160. imagedestroy($nimage);
  161. imagedestroy($simage);
  162. }
  163. if($imgpreview==1)
  164. {
  165. echo "<br>图片预览:<br>";
  166. echo "<img src=\"".$destination."\" width=".($image_size[0]*$imgpreviewsize)." height=".($image_size[1]*$imgpreviewsize);
  167. echo " alt=\"图片预览:\r文件名:".$destination."\r上传时间:\">";
  168. }
  169. }
  170. ?>
  171. </body>
  172. </html>