手冊中摘取的幾句話:php
- 當 HTTP POST 請求的 Content-Type 是 application/x-www-form-urlencoded 或 multipart/form-data 時,會將變量以關聯數組形式傳入當前腳本。
- php://input 是個能夠訪問請求的原始數據的只讀流。 enctype="multipart/form-data" 的時候php://input 是無效的。
驗證下:html
post.html數組
<!DOCTYPE html> <html> <head> <title></title> </head> <body> <form action="getpost.php" method="post"> <input type="text" name="name" value="saisai"> <input type="submit" name="submit" value="submit"> </form> </body> </html>
getpost.phpapp
<?php echo "----------input--------<br />"; var_dump(file_get_contents('php://input', 'r')); echo "----------post---------<br />"; var_dump($_POST); ?>
1、enctype="application/x-www-form-urlencoded"post
請求主體:url
Content-Type: application/x-www-form-urlencoded Content-Length: 25 name=saisai&submit=submit
輸出:spa
----------input-------- string 'name=saisai&submit=submit' (length=25) ----------post--------- array (size=2) 'name' => string 'saisai' (length=6) 'submit' => string 'submit' (length=6)
小結:當enctype="application/x-www-form-urlencoded"時,請求主體(request body)中的數據(name=saisai&submit=submit)轉換成關聯數組放入$_POST,而 php://input 則獲取的是原始數據(raw data)。code
2、enctype=「multipart/form-data」時orm
2.1 表單:htm
<form action="getpost.php" method="post" enctype="multipart/form-data"> <input type="text" name="name" value="saisai"> <input type="submit" name="submit" value="submit"> </form>
請求主題:
Content-Type: multipart/form-data; boundary=---------------------------22554656810024
Content-Length: 249
-----------------------------22554656810024
Content-Disposition: form-data; name="name"
saisai
-----------------------------22554656810024
Content-Disposition: form-data; name="submit"
submit
-----------------------------22554656810024--
輸出:
----------input-------- string '' (length=0) ----------post--------- array (size=2) 'name' => string 'saisai' (length=6) 'submit' => string 'submit' (length=6)
小結:在enctype="multipart/form-data" 且沒有上傳文件控件時,$_POST 能正常打印數據,php:// 無效。
2.2 表單(添加一個文件上傳):
<form action="getpost.php" method="post" enctype="multipart/form-data"> <input type="text" name="name" value="saisai"> <input type="submit" name="submit" value="submit"> </form>
請求主題:
Content-Type: multipart/form-data; boundary=---------------------------272321281228527
Content-Length: 68386
-----------------------------272321281228527
Content-Disposition: form-data; name="name"
saisai
-----------------------------272321281228527
Content-Disposition: form-data; name="filename"; filename="dog.png"
Content-Type: image/png
一堆亂碼
-----------------------------272321281228527
Content-Disposition: form-data; name="submit"
submit
-----------------------------272321281228527--
輸出:
----------input-------- string '' (length=0) ----------post--------- array (size=2) 'name' => string 'saisai' (length=6) 'submit' => string 'submit' (length=6)
小結:在enctype="multipart/form-data" 且有上傳文件控件時,$_POST 能打印出傳入的數據,可是排除了上傳的任何內容。php:// 無效。
3、enctype="text/plain"
表單:
<form action="getpost.php" method="post" enctype="text/plain"> <input type="text" name="name" value="saisai"> <input type="submit" name="submit" value="submit"> </form>
請求主體:
Content-Type: text/plain
Content-Length: 28
name=saisai
submit=submit
輸出:
----------input-------- string 'name=saisai submit=submit ' (length=28) ----------post--------- array (size=0) empty
小結:enctype="text/plain"時,$_POST中沒有內容,php://input中以鍵值對的方式存放。
總結: