大网数据广告活动
PHP 语言与运行手册

request_parse_body · Read and parse the request body and return the result

· 52CE 我爱测

request_parse_body — Read and parse the request body and return the result

request_parse_body

(PHP 8 >= 8.4.0)

request_parse_body — Read and parse the request body and return the result

Description

function request_parse_body(?array $options = null): array

This function reads the request body and parses it according to the Content-Type header. Currently, two content types are supported:

  • application/x-www-form-urlencoded

  • multipart/form-data

This function is used primarily to parse multipart/form-data requests with HTTP verbs other than POST which do not automatically populate the $_POST and $_FILES superglobals.

Caution

The request body can only be consumed once. request_parse_body() consumes the request body without buffering it to the php://input stream. Conversely, if the body has already been read (e.g. via php://input), request_parse_body() will return empty data.

Parameters

options

The options parameter accepts an associative array to override the following global php.ini settings for parsing of the request body.

  • max_file_uploads

  • max_input_vars

  • max_multipart_body_parts

  • post_max_size

  • upload_max_filesize

Return Values

request_parse_body() returns an array pair with the equivalent of $_POST at index 0 and $_FILES at index 1.

Errors/Exceptions

When the request body is invalid, according to the Content-Type header, a RequestParseBodyException is thrown.

A ValueError is thrown when options contains invalid keys, or invalid values for the corresponding key.

Examples

Example #1 request_parse_body() example

<?php
// Parse request and store result in the $_POST and $_FILES superglobals.
[$_POST, $_FILES] = request_parse_body();
// Echo the content of some transferred file
echo file_get_contents($_FILES['file_name']['tmp_name']);
?>

Example #2 request_parse_body() example with customized options

<?php
// form.php

assert_logged_in();

// Only for this form, we allow a bigger upload size.
[$_POST, $_FILES] = request_parse_body([
    'post_max_size' => '10M',
    'upload_max_filesize' => '10M',
]);

// Do something with the uploaded files.
?>

来源:PHP Documentation Group · 作者与贡献记录 · CC BY 3.0。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
PHP Documentation Group
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。