# 输入流 php\://input

## $\_POST 与 php\://input

1. 仅在取值为`application/x-www-data-urlencoded`和`multipart/form-data`时(文件上传时)，php会将http请求body相应数据会填入到数组$\_POST，填入到$\_POST数组中的数据是进行urldecode()解析的结果。
2. 只要Content-Type不为`multipart/form-data`， `php://input`会填入post数据。
3. 仅当Content-Type为`application/x-www-form-urlencoded`且提交方法是POST方法时，$\_POST数据与`php://input`数据才是一致的。

## $HTTP\_RAW\_POST\_DATA 与 php\://input

`php://input`可以读取没有处理过的POST数据。相较于$HTTP\_RAW\_POST\_DATA而言，它给内存带来的压力较小。

$HTTP\_RAW\_POST\_DATA（This feature has been DEPRECATED as of PHP 5.6.0. 被废弃了，查看[官方文档](https://secure.php.net/manual/zh/reserved.variables.httprawpostdata.php)）

更多php\://input，查看[官方文档](https://secure.php.net/manual/zh/wrappers.php.php#wrappers.php.input)

> 解析
>
> ```php
> var_dump($_POST);
> echo file_get_contents("php://input");
> ```
>
> 情况1: 文件上传时,form的`enctype="multipart/form-data",`此时,数据`php://input`获取不到数据
>
> ```php
> <form enctype="multipart/form-data" method="post">
> <input type="text" name="name" />
> <input type="file" name="csv_file" />
> <button type="submit" name="submit" value="Submit">Submit</button>
> </form>
> <?php
> var_dump($_POST);
> echo "<br>";
> var_dump(file_get_contents("php://input"));
> ```
>
> 测试结果 只有`$_POST`获取了数据
>
> array (size=2)
>
> 'name' => string 'test' (length=4)
>
> 'submit' => string 'Submit' (length=6)
>
> 情况2: 非文件上传时的form表单
>
> ```php
> <form method="post">
> name:<input type="text" name="name" /><br>
> age:<input type="text" name="age" />
> <button type="submit" name="submit" value="Submit">Submit</button>
> </form>
> <?php
> echo '$_POST result:<br>';
> var_dump($_POST);
> echo "<br>";
> echo 'php://input result:<br>';
> var_dump(file_get_contents("php://input"));
> ```
>
> 测试结果:两者都会获取数据
>
> ```php
> $_POST result:
> array (size=3)
>   'name' => string 'revin' (length=5)
>   'age' => string '28' (length=2)
>   'submit' => string 'Submit' (length=6)
>
> php://input result:
> string 'name=revin&age=28&submit=Submit' (length=31)
> ```
>
> 情况3 :postman 直接发送json body 体, 也就是api的场景
>
> ![](/files/-LfnTK0Aojwq9hCvM9wx)
>
> 测试结果:只有`php://input`获取到了数据&#x20;


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://phper.shujuwajue.com/shu-zu/shu-ru-liu-php-input.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
