服務器需要將發送的多媒體資料的型別告訴瀏覽器,而告訴瀏覽器的手段就是告知多媒體的MIME型別,
form表單中的enctype屬性,可以告訴服務器,我們提供給它的內容的MIME型別,
enctype屬性有三種狀態值:
1). application/x-www-form-urlencoded
資料發送到服務器之前,所有字符都會進行編碼(空格轉換為 "+" 加號,特殊符號轉換為 ASCII HEX 值)
2). multipart/form-data
不對字符編碼,在使用包含檔案上傳控制元件的表單時,必須使用該值,
瀏覽器會把整個表單以控制元件為單位分割,并為每個部分加上Content-Disposition(form-data或者file),Content-Type(默認為text/plain),name(控制元件name)等資訊,并加上分割符(boundary),
如使用該數值表單資訊以二進制形式發送,
3). text/plain
空格轉換為 "+" 加號,但不對特殊字符編碼
新建代碼檔案:
#multipart/form-data格式提交表單 [root@VM_0_11_centos localhost]# touch form_data.html && vim form_data.html #錄入以下內容: <html> <meta charset="utf-8"/> <body> <form enctype="multipart/form-data" action="/post.php" method="post"> <input type="text" style="width:320px;" name ="test"/> <input type="submit" value="https://www.cnblogs.com/wscsq789/p/提交" /> </form> </body> </html> #form-urlencode型別提交表單 [root@VM_0_11_centos localhost]# touch form_urlencode.html && vim form_urlencode.html #錄入以下內容 <html> <meta charset="utf-8"/> <body> <form enctype="application/x-www-form-urlencoded" action="/post.php" method="post"> <input type="text" style="width:320px;" name ="test"/> <input type="submit" value="https://www.cnblogs.com/wscsq789/p/提交" /> </form> </body> </html> #text/plain型別提交表單 [root@VM_0_11_centos localhost]# touch form_text_plain.html && vim form_text_plain.html #錄入以下內容 <html> <meta charset="utf-8"/> <body> <form enctype="text/plain" action="/post.php" method="post"> <input type="text" style="width:320xp;" name ="test"/> <input type="submit" value="https://www.cnblogs.com/wscsq789/p/提交" /> </form> </body> </html>
#新建資料接收php檔案 [root@VM_0_11_centos localhost]# touch post.php && vim post.php #錄入以下內容 <?php echo("<pre>"); echo('content-type: ' . $_SERVER['CONTENT_TYPE'] . PHP_EOL . PHP_EOL); $data = file_get_contents("php://input"); echo("input: " . $data); echo(PHP_EOL . PHP_EOL); echo("array:" . PHP_EOL); print_r($_POST);
1.form_data.html
前端頁面錄入資料:

提交表單:

2. form-urlencode:
前端頁面錄入資料:

提交表單:

3. text/plain:
前端頁面錄入:

提交表單:

通過上面三種輸出測驗可以看出:
1. form-data型別表單提交,全域變數$_POST可以獲取資料,php://input無法進行流讀取,特殊字符未做轉碼
2. from-urlencode型別表單提交,全域變數$_POST和流讀取均可獲取資料,流讀取方式被urlencode轉碼,空格以+號代替
3. text/plain型別表單提交,全域變數$_POST無法獲取資料,流讀取資料正常,特殊字符未被轉碼,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/21631.html
標籤:Html/Css
