手冊中摘取的幾句話:
- 當 HTTP POST 請求的 Content-Type 是 application/x-www-form-urlencoded 或 multipart/form-data 時,會將變量以關聯數組形式傳入當前腳本。
- php://input 是個可以訪問請求的原始數據的只讀流。 enctype="multipart/form-data" 的時候php://input 是無效的。
驗證下:
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.php
<?php echo "----------input--------<br />"; var_dump(file_get_contents('php://input', 'r')); echo "----------post---------<br />"; var_dump($_POST); ?>
一、enctype="application/x-www-form-urlencoded"
請求主體:
Content-Type: application/x-www-form-urlencoded Content-Length: 25 name=saisai&submit=submit
輸出:
----------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)。
二、enctype=“multipart/form-data”時
2.1 表單:
<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:// 無效。
三、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中以鍵值對的方式存放。
總結:
- 當 HTTP POST 請求的 Content-Type 是 application/x-www-form-urlencoded 或 multipart/form-data :php://input 中是形同 a=1&b=2的原始數據。$_POST 中是關聯數組,且沒有上傳控件的內容。
- php://input 是個可以訪問請求的原始數據的只讀流。 enctype="multipart/form-data" 的時候php://input 是無效的。
- $_POST 不能獲取 Content-Type = "text/plain"時 post的數據, php://input可以。