PHP上传文件

  1<?php
  2header("content-Type: text/html; charset=gb2312");
  3$uptypes=array('image/jpg',  //上传文件类型列表
  4 'image/jpeg',
  5 'image/png',
  6 'image/pjpeg',
  7 'image/gif',
  8 'image/bmp',
  9 'application/x-shockwave-flash',
 10 'image/x-png',
 11 'application/msword',
 12 'audio/x-ms-wma',
 13 'audio/mp3',
 14 'application/vnd.rn-realmedia',
 15 'application/x-zip-compressed',
 16 'application/octet-stream');
 17
 18$max_file_size=2000000000;   //上传文件大小限制, 单位BYTE
 19$path_parts=pathinfo($_SERVER['PHP_SELF']); //取得当前路径
 20$destination_folder="up/"//上传文件路径
 21$watermark=0;   //是否附加水印(1为加水印,其他为不加水印);
 22$watertype=1;   //水印类型(1为文字,2为图片)
 23$waterposition=1;   //水印位置(1为左下角,2为右下角,3为左上角,4为右上角,5为居中);
 24$waterstring="newphp.site.cz"//水印字符串
 25$waterimg="xplore.gif";  //水印图片
 26$imgpreview=1;   //是否生成预览图(1为生成,其他为不生成);
 27$imgpreviewsize=1/2;  //缩略图比例
 28?>
 29<html>
 30<head>
 31<title>『幻γ野D风』- Fsky.Net.Ru</title>
 32<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
 33<meta name="keywords" content="免费上传,文件上传,免费上传图片,图片上传,文件上传系统,免费传文件,免费传图片,幻γ野D风_文件上传,免费" />
 34<meta name="description" content="幻γ野D风_文件上传" />
 35<style type="text/css">body,td{font-family:tahoma,verdana,arial;font-size:11px;line-height:15px;background-color:white;color:#666666;margin-left:20px;}
 36strong{font-size:12px;}
 37a:link{color:#0066CC;}
 38a:hover{color:#FF6600;}
 39a:visited{color:#003366;}
 40a:active{color:#9DCC00;}
 41a{TEXT-DECORATION:none}
 42table.itable{}
 43td.irows{height:20px;background:url("index.php?i=dots") repeat-x bottom}</style>
 44</head>
 45<script type="text/javascript">function oCopy(obj){obj.select();js=obj.createTextRange();js.execCommand("Copy");};function sendtof(url){window.clipboardData.setData('Text',url);alert('复制地址成功,粘贴给你好友一起分享。');};function select_format(){var on=document.getElementById('fmt').checked;document.getElementById('site').style.display=on?'none':'';document.getElementById('sited').style.display=!on?'none':'';};var flag=false;function DrawImage(ImgD){var image=new Image();image.src=ImgD.src;if(image.width>0&&image.height>0){flag=true;if(image.width/image.height>=120/80){if(image.width>120){ImgD.width=120;ImgD.height=(image.height*120)/image.width;}else {ImgD.width=image.width;ImgD.height=image.height;};ImgD.alt=image.width+"×"+image.height;}else {if(image.height>80){ImgD.height=80;ImgD.width=(image.width*80)/image.height;}else {ImgD.width=image.width;ImgD.height=image.height;};ImgD.alt=image.width+"×"+image.height;}};};function FileChange(Value){flag=false;document.all.uploadimage.width=10;document.all.uploadimage.height=10;document.all.uploadimage.alt="";document.all.uploadimage.src=Value;};</script>
 46<body>
 47<center><form enctype="multipart/form-data" method="post" name="upform">
 48    <div style="position: absolute; width: 120px; height: 600; z-index: 1; left: 20px; top: 0px" id="layer1"><script type="text/javascript"><!--
 49google_ad_client = "pub-5019418081657321";
 50google_ad_width = 160;
 51google_ad_height = 600;
 52google_ad_format = "160x600_as";
 53google_ad_type = "text_image";
 54google_ad_channel = "";
 55//-->
 56</script>
 57<script type="text/javascript"
 58  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
 59</script>
 60</div><table border="1" width="55%" id="table1" cellspacing=0><tr><td colspan="2" style="font-family: tahoma,verdana,arial; font-size: 11px; line-height: 15px; color: #666666; margin-left: 20px; background-color: white"><p align="center"><a title="幻γ野D风" href="http://fsky.net.ru" target="_blank">
 61幻γ野D风</a>上传文件:</td></tr><tr><td style="font-family: tahoma,verdana,arial; font-size: 11px; line-height: 15px; color: #666666; margin-left: 20px; background-color: white" width="10%"><div style="width:120px; height:80px;overflow:hidden;text-align: center;" ><IMG id=uploadimage height=0 width=0 src=""  onload="javascript:DrawImage(this);" ></div></td><td style="font-family: tahoma,verdana,arial; font-size: 11px; line-height: 15px; color: #666666; margin-left: 20px; background-color: white" width="71%"><div style="width:361px; height:80px;overflow:hidden;text-align: center;padding:30px; " ><input style="width:208;border:1 solid #9a9999; font-size:9pt; background-color:#ffffff; height:18" size="17" name=upfile type=file 
 62onchange="javascript:FileChange(this.value);"><input type="submit" value="上传" style="width:60;border:1 solid #9a9999; font-size:9pt; background-color:#ffffff; height:18" size="17"></td></tr></div></table>允许上传的文件类型为:jpg|jpeg|gif|bmp|png|swf|mp3|wma|zip|rar|doc<br>
 63<div style="position: absolute; width: 120px; height: 600; z-index: 1; left: 690px; top: 0px" id="layer2"><script type="text/javascript"><!--
 64google_ad_client = "pub-5019418081657321";
 65google_ad_width = 160;
 66google_ad_height = 600;
 67google_ad_format = "160x600_as";
 68google_ad_type = "text_image";
 69google_ad_channel = "";
 70//-->
 71</script>
 72<script type="text/javascript"
 73  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
 74</script>
 75 </div>
 76    <p><br>
 77    </p>
 78</form>
 79
 80<?php
 81if ($_SERVER['REQUEST_METHOD'== 'POST')
 82{
 83if (!is_uploaded_file($_FILES["upfile"][tmp_name]))
 84//是否存在文件
 85{
 86echo "<font color='red'>文件不存在!</font>";
 87exit;
 88}
 89
 90 $file = $_FILES["upfile"];
 91 if($max_file_size < $file["size"])
 92 //检查文件大小
 93 {
 94 echo "<font color='red'>文件太大!</font>";
 95 exit;
 96  }
 97
 98if(!in_array($file["type"], $uptypes))
 99//检查文件类型
100{
101 echo "<font color='red'>不能上传此类型文件!</font>";
102 exit;
103}
104
105if(!file_exists($destination_folder))
106mkdir($destination_folder);
107
108$filename=$file["tmp_name"];
109$image_size = getimagesize($filename);
110$pinfo=pathinfo($file["name"]);
111$ftype=$pinfo[extension];
112$destination = $destination_folder.time().".".$ftype;
113if (file_exists($destination&& $overwrite != true)
114{
115     echo "<font color='red'>同名文件已经存在了!</a>";
116     exit;
117  }
118
119 if(!move_uploaded_file ($filename, $destination))
120 {
121   echo "<font color='red'>移动文件出错!</a>";
122     exit;
123  }
124
125$pinfo=pathinfo($destination);
126$fname=$pinfo[basename];
127echo " <font color=red>成功上传,鼠标移动到地址栏自动复制</font><br><table width=\"348\" cellspacing=\"0\" cellpadding=\"5\" border=\"0\" class=\"table_decoration\" align=\"center\"><tr><td><input type=\"checkbox\" id=\"fmt\" onclick=\"select_format()\"/>图片UBB代码<br/><div id=\"site\"><table border=\"0\"><tr><td valign=\"top\">文件地址:</td><td><input type=\"text\" onclick=\"sendtof(this.value)\" onmouseover=\"oCopy(this)\" style=font-size=9pt;color:blue size=\"44\" value=\"http://".$_SERVER['SERVER_NAME'].$path_parts["dirname"]."/".$destination_folder.$fname."\"/>
128</td></tr></table></div><div id=\"sited\" style=\"display:none\"><table border=\"0\"><tr><td valign=\"top\">文件地址:</td><td><input type=\"text\" onclick=\"sendtof(this.value)\" onmouseover=\"oCopy(this)\" style=font-size=9pt;color:blue size=\"44\" value=\"[img]http://".$_SERVER['SERVER_NAME'].$path_parts["dirname"]."/".$destination_folder.$fname."[/img]\"/></td></tr></table></div></td></tr></table>";
129echo " 宽度:".$image_size[0];
130echo " 长度:".$image_size[1];
131if($watermark==1)
132{
133$iinfo=getimagesize($destination,$iinfo);
134$nimage=imagecreatetruecolor($image_size[0],$image_size[1]);
135$white=imagecolorallocate($nimage,255,255,255);
136$black=imagecolorallocate($nimage,0,0,0);
137$red=imagecolorallocate($nimage,255,0,0);
138imagefill($nimage,0,0,$white);
139switch ($iinfo[2])
140{
141 case 1:
142 $simage =imagecreatefromgif($destination);
143 break;
144 case 2:
145 $simage =imagecreatefromjpeg($destination);
146 break;
147 case 3:
148 $simage =imagecreatefrompng($destination);
149 break;
150 case 6:
151 $simage =imagecreatefromwbmp($destination);
152 break;
153 default:
154 die("<font color='red'>不能上传此类型文件!</a>");
155 exit;
156}
157
158imagecopy($nimage,$simage,0,0,0,0,$image_size[0],$image_size[1]);
159imagefilledrectangle($nimage,1,$image_size[1]-15,80,$image_size[1],$white);
160
161switch($watertype)
162{
163 case 1:  //加水印字符串
164 imagestring($nimage,2,3,$image_size[1]-15,$waterstring,$black);
165 break;
166 case 2:  //加水印图片
167 $simage1 =imagecreatefromgif("xplore.gif");
168 imagecopy($nimage,$simage1,0,0,0,0,85,15);
169 imagedestroy($simage1);
170 break;
171}
172
173switch ($iinfo[2])
174{
175 case 1:
176 //imagegif($nimage, $destination);
177 imagejpeg($nimage, $destination);
178 break;
179 case 2:
180 imagejpeg($nimage, $destination);
181 break;
182 case 3:
183 imagepng($nimage, $destination);
184 break;
185 case 6:
186 imagewbmp($nimage, $destination);
187 //imagejpeg($nimage, $destination);
188 break;
189}
190
191//覆盖原上传文件
192imagedestroy($nimage);
193imagedestroy($simage);
194}
195
196if($imgpreview==1)
197{
198echo "<br>图片预览:<br>";
199echo "<a href=\"".$destination."\" target='_blank'><img src=\"".$destination."\" width=".($image_size[0]*$imgpreviewsize)." height=".($image_size[1]*$imgpreviewsize);
200echo " alt=\"图片预览:\r文件名:".$fname."\r上传时间:".date('m/d/Y h:i')."\" border='0'></a>";
201}
202}
203?>
204</center>
205</body>
206</html>

posted on 2009-04-16 15:32 郑桂斌 阅读(724) 评论(0)  编辑 收藏 引用 网摘 所属分类: PHP学习第一步:


只有注册用户登录后才能发表评论。
网站导航:
<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

导航

统计

留言簿(2)

随笔分类

随笔档案

文章分类

文章档案

最新随笔

搜索

最新随笔

最新评论

阅读排行榜

评论排行榜