PHP開發(fā)之簡單文件上傳到MySql數(shù)據(jù)庫(二)
本節(jié)講解通過表單和表格來組合一個文件上傳簡單前端頁面
<form>?標(biāo)簽的?enctype?屬性規(guī)定了在提交表單時要使用哪種內(nèi)容類型。在表單需要二進(jìn)制數(shù)據(jù)時,比如文件內(nèi)容,請使用 "multipart/form-data"。
<input>?標(biāo)簽的?type="file"?屬性規(guī)定了應(yīng)該把輸入作為文件來處理。舉例來說,當(dāng)在瀏覽器中預(yù)覽時,會看到輸入框旁邊有一個瀏覽按鈕。
這里我們使用了<input?type="hidden" >隱藏域來限制上傳文件的大小
<form> <input type="hidden" name="MAX_FILE_SIZE" value="2000000"> </form>
隱藏域在頁面中對于用戶是不可見的,在表單中插入隱藏域的目的在于收集或發(fā)送信息,以利于被處理表單的程序所使用。瀏覽者單擊發(fā)送按鈕發(fā)送表單的時候,隱藏域的信息也被一起發(fā)送到服務(wù)器。
<table>標(biāo)簽這里使用了兩個屬性cellspacing cellpadding
單元格邊距(表格填充)(cellpadding) -- 代表單元格外面的一個距離,用于隔開單元格與單元格空間
單元格間距(表格間距)(cellspacing) -- 代表表格邊框與單元格補(bǔ)白的距離,也是單元格補(bǔ)白之間的距離
在這里設(shè)置為cellspacing=0,cellpadding=0
下面展示下完整頁面代碼:
<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>