PHP開(kāi)發(fā)之簡(jiǎn)單檔案上傳到MySql資料庫(kù)(二)
本節(jié)講解透過(guò)表單和表格來(lái)組合一個(gè)檔案上傳簡(jiǎn)單前端頁(yè)面
<form>?標(biāo)籤的?enctype?屬性規(guī)定了在提交表單時(shí)要使用哪種內(nèi)容類(lèi)型。表單需要二進(jìn)位資料時(shí),例如檔案內(nèi)容,請(qǐng)使用 "multipart/form-data"。
<input>?標(biāo)籤的?type="file"?屬性規(guī)定了應(yīng)該把輸入當(dāng)作檔案來(lái)處理。舉例來(lái)說(shuō),當(dāng)在瀏覽器中預(yù)覽時(shí),會(huì)看到輸入框旁邊有一個(gè)瀏覽按鈕。
這裡我們使用了<input?type="hidden" >隱藏網(wǎng)域來(lái)限制上傳檔案的大小
<form> <input type="hidden" name="MAX_FILE_SIZE" value="2000000"> </form>
隱藏網(wǎng)域在頁(yè)面中對(duì)於使用者是不可見(jiàn)的,在表單中插入隱藏域的目的在於收集或傳送訊息,以利被處理表單的程式所使用。瀏覽者點(diǎn)擊傳送按鈕發(fā)送表單的時(shí)候,隱藏網(wǎng)域的資訊也被一起傳送到伺服器。
<table>標(biāo)籤這裡使用了兩個(gè)屬性cellspacing cellpadding
單元格邊距(表格填充)(cellpadding) -- 代表單元格外面的一個(gè)距離,用於隔開(kāi)單元格與單元格空間
單元格間距(表格間距)(cellspacing) -- 代表表格邊框與單元格補(bǔ)白的距離,也是單元格補(bǔ)白之間的距離
在這裡設(shè)定為cellspacing=0,cellpadding=0
下方顯示完整頁(yè)面程式碼:
<html> <head> <meta charset="utf-8"> <title>文件上傳實(shí)例</title> <style type="text/css"> <!-- body { font-size: 20px; } input { background-color: #66CCFF; border: 1px inset #CCCCCC; } form { margin-top:5%; } --> </style> </head> <body> <form method="post" action="?action=save" enctype="multipart/form-data"> <table border=0 cellspacing=0 cellpadding=0 align=center width="100%"> <tr> <td width=55 height=20 align="center"></td> <td height="16"> <table> <tr> <td>標(biāo)題:</td> <td><input name="title" type="text" id="title"></td> </tr> <tr> <td>文件: </td> <td><label> <input name="file" type="file" value="瀏覽" > <input type="hidden" name="MAX_FILE_SIZE" value="2000000"> </label></td> </tr> <tr> <td></td> <td><input type="submit" value="上 傳" name="upload"></td> </tr> </table> </td> </tr> </table> </form> </body> </html>