學(xué)習(xí)筆記:PHP上傳圖片代碼詳解
想知道上傳圖片代碼怎么寫嗎,下面我就帶大家一起詳細(xì)分析一下吧。利用PHP,你總是可以有多種方式來(lái)完成某個(gè)特定的任務(wù)。我們就拿文件上傳舉個(gè)例子。當(dāng)然了,你可以按照傳統(tǒng)的方式來(lái)使用HTTP文件上傳,把文件直接傳輸?shù)絎eb服務(wù)器磁盤上。
#T#你還可以用更加奇異的方式上傳,用FTP協(xié)議兩步就完成上傳:從你的本地硬盤到Web服務(wù)器,然后再到FTP服務(wù)器。PHP在本機(jī)同時(shí)支持FTP和HTTP上傳,所以你可以根據(jù)自己應(yīng)用程序的設(shè)計(jì)需要進(jìn)行最佳的選擇。使用PHP的FTP函數(shù)進(jìn)行文件傳輸幾乎與使用傳統(tǒng)的FTP客戶端相同——你會(huì)看到連函數(shù)的名字都和標(biāo)準(zhǔn)的FTP命令類似。和大家分享一下PHP上傳圖片代碼的小例子,希望大家多多提意見嘿嘿謝謝了一起學(xué)習(xí)!!
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('上傳類型不對(duì)');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\\">";
- }}}
- ?>