想知道上傳圖片代碼怎麼寫嗎,下面我就帶大家一起詳細分析一下吧。利用PHP,你總是可以有多種方式來完成某個特定的任務。我們就拿文件上傳舉個例子。當然了,你可以按照傳統的方式來使用HTTP文件上傳,把文件直接傳輸到Web服務器磁盤上。
PHP上傳圖片代碼:
- <?phpsession_start();?>
- <?php
- $id=mysql_connect('localhost','root','585858');
- mysql_select_db("okhwyy",$id);
- mysql_query("setnamesgb2312");
- ?>
- <html>
- <head>
- <metahttp-equivmetahttp-equiv="Content-Type"c>
- <title>限制上傳圖片的格式</title>
- <styletypestyletype="text/css">
- <!--
- .style1{
- font-size:14px;
- font-family:"華文行楷";
- }
- .style4{font-size:12px;font-weight:bold;}
- -->
- </style>
- </head>
- <body>
- <tablewidthtablewidth="406"height="129"border="0"align="center"cellpadding="0"cellspacing="0"background="">
- <tr>
- <tdwidthtdwidth="106"height="40"></td>
- <tdwidthtdwidth="196"></td>
- <tdwidthtdwidth="31"></td>
- </tr>
- <formnameformname="form1"method="post"action=""enctype="multipart/form-data">
- <tr>
- <tdheighttdheight="32"align="right"><spanclassspanclass="style1">圖片路徑</span>:</td>
- <tdvaligntdvalign="middle"><inputnameinputname="images"type="file"id="images2"size="15">
- <inputtypeinputtype="hidden"name="MAX_FILE_SIZE"value="30000"></td>
- <td></td>
- </tr>
- <tr>
- <tdheighttdheight="44"align="right"valign="middle"><spanclassspanclass="style4">圖片的格式</span>:</td>
- <tdvaligntdvalign="middle"><spanclassspanclass="style4">(.jpg)</span><inputtypeinputtype="submit"name="Submit"value="提交"></td>
- <td></td>
- </tr>
- </form>
- <tr>
- <tdheighttdheight="10"></td>
- <td></td>
- <td></td>
- </tr>
- </table>
- <tablewidthtablewidth="406"height="129"border="1"align="center"cellpadding="0"cellspacing="0">
- <?php
- $query="select*fromtb_image2whereidorderbydatadesclimit2";
- $result=mysql_query($query);
- if($result){
- while($row=mysql_fetch_array($result)){
- ?>
- <tr>
- <tdwidthtdwidth="106"align="center"><?phpecho$row[data];?></td>
- <tdwidthtdwidth="196"align="center"><imgsrcimgsrc="<?phpecho$row[path];?>"width="200"height="120"></td>
- </tr>
- <?php}}?>
- </table>
- </body>
- </html>
- <?php
- $Submit=$_POST[Submit];
- if($Submit){
- $image=$_FILES['images']['name'];
- $datedate=date("Y-m-d");
- $path="upfiles/".$_FILES['images']['name'];
- $type=strstr($path,".");
- $size=$_FILES['images']['size'];
- if($size>1000000){echo"<script>alert('上傳容量超限');history.back();</script>";}
- elseif($type!=".jpg"){echo"<script>alert('上傳類型不對');history.back();</script>";}
- elseif(move_uploaded_file($_FILES['images']['tmp_name'],$path)){
- $query="insertintotb_image2(image_name,path,data)values('$image','$path','$date')";
- $result=mysql_query($query)ordie(mysql_error());
- if($result){
- echo"上傳成功!";
- echo"<metahttp-equivmetahttp-equiv=\"Refresh\"content=\"3;url=index.php\">";
- }
- else{
- echo"上傳失敗!";
- echo"<metahttp-equivmetahttp-equiv=\"Refresh\"content=\"3;url=index.php\">";
- }}}
- ?>