PHP request_parse_body() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Network

Definition

In PHP, request_parse_body() reads the raw HTTP request body and parses application/x-www-form-urlencoded or multipart/form-data data into a structure like $_POST and $_FILES.

It is available in PHP 8.4+. The main use is parsing multipart bodies for verbs other than POST, which do not fill the normal superglobals automatically. It returns a two-item array: index 0 is the post fields, index 1 is the files list.

Syntax

Call request_parse_body() with the values you need. Start with this line.

PHP
[$_POST, $_FILES] = request_parse_body();
CallWhat it does
request_parse_body()Parses the current request body with the default php.ini limits.
request_parse_body(['post_max_size' => '10M'])Overrides selected upload and body size settings for this parse only.
[$post, $files] = request_parse_body()Destructures the [post, files] pair into two variables.

The optional $options array may override keys such as post_max_size, upload_max_filesize, max_file_uploads, max_input_vars, and max_multipart_body_parts. The body can be consumed only once; reading php://input first can leave this function empty.

Library declarations
PHP
request_parse_body(?array $options = null): array

Parameters

ParameterDescription
optionsOptional. Associative array of php.ini-style limits for this parse, or null to use the global settings.

Return Value

Return ValueDescription
arrayA two-element array: [0] post fields (like $_POST), [1] uploaded files (like $_FILES).

PHP Examples

Example 1 — Parse a Form Body into Post and Files

Show the usual destructuring call. The View Output below is a fixed sample of a successful parse.

PHP
<?php
// Typical usage inside a PHP 8.4+ request handler:
[$post, $files] = request_parse_body();

echo 'name: ', $post['name'] ?? '', PHP_EOL;
echo 'file field: ', isset($files['photo']) ? 'yes' : 'no', PHP_EOL;

How It Works

  1. request_parse_body() reads and parses the request body.
  2. Index 0 holds regular fields, index 1 holds file metadata.
  3. Destructuring assigns those parts to $post and $files.
  4. The printed values represent a successful multipart sample.

Example 2 — Raise Upload Limits for One Form

Pass an options array so one endpoint allows a larger body without changing php.ini globally.

PHP
<?php
[$post, $files] = request_parse_body([
    'post_max_size' => '10M',
    'upload_max_filesize' => '10M',
]);

echo 'title: ', $post['title'] ?? '', PHP_EOL;
echo 'bytes: ', $files['doc']['size'] ?? 0, PHP_EOL;

How It Works

  1. The options array overrides selected ini settings for this call.
  2. post_max_size and upload_max_filesize use shorthand sizes like 10M.
  3. Returned files entries look like classic $_FILES rows.
  4. Sample output shows a title field and a 2048-byte upload.

Example 3 — Keep the Pair Without Touching Superglobals

Store the parse result in local variables when you do not want to overwrite $_POST and $_FILES.

PHP
<?php
$result = request_parse_body();
$post = $result[0];
$files = $result[1];

echo 'fields: ', count($post), PHP_EOL;
echo 'files: ', count($files), PHP_EOL;
echo 'has email: ', array_key_exists('email', $post) ? 'yes' : 'no', PHP_EOL;

How It Works

  1. Assign the full array first if you prefer indexed access.
  2. count() reports how many fields and file keys were parsed.
  3. array_key_exists() checks for a named field safely.
  4. This sample body had two fields and no uploads.

Frequently Asked Questions

It reads the HTTP request body and parses urlencoded or multipart form data into a [post, files] array pair.
PHP 8.4. Older versions do not include this function.
Non-POST verbs do not populate $_POST and $_FILES automatically for multipart bodies. This function parses those bodies on demand.
The body is consumed once. A second call usually sees empty data if the stream was already read.
RequestParseBodyException for an invalid body, and ValueError for bad option keys or values.

Next: setcookie()

setcookie() queues a Set-Cookie header so the browser stores a small name/value pair and s.

setcookie() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful