服務器須要將發送的多媒體數據的類型告訴瀏覽器,而告訴瀏覽器的手段就是告知多媒體的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 空格轉換爲 "+" 加號,但不對特殊字符編碼
新建代碼文件:php
#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="提交" /> </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="提交" /> </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="提交" /> </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.htmlhtml
前端頁面錄入數據:前端
提交表單:vim
2. form-urlencode:centos
前端頁面錄入數據:瀏覽器
提交表單:服務器
3. text/plain:app
前端頁面錄入:post
提交表單:測試
經過上面三種輸出測試能夠看出:
1. form-data類型表單提交,全局變量$_POST能夠獲取數據,php://input沒法進行流讀取,特殊字符未作轉碼
2. from-urlencode類型表單提交,全局變量$_POST和流讀取都可獲取數據,流讀取方式被urlencode轉碼,空格以+號代替
3. text/plain類型表單提交,全局變量$_POST沒法獲取數據,流讀取數據正常,特殊字符未被轉碼。