wppaste
WordPress

WP_REST_Attachments_Controller::upload_from_data( string $data, array $headers, string|null $time = null ): array|WP_Error

Since
4.7.0, 6.6.0
Source
wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php:1180
Handles an upload via raw POST data.

Compatibility

WordPress
since 6.6.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$datastring
Supplied file data.
$headersarray
HTTP headers from the request.
$timestring|nulloptional
Time formatted in 'yyyy/mm'. Default null.Default: null

Return value

array|WP_Error
Data from wp_handle_sideload().

Performance profile

How much work a call to WP_REST_Attachments_Controller::upload_from_data() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reads or writes the filesystem via fopen().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
14–92

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 148.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • filesystemfilesystem accessfopen()called directly
  • hookthird-party callbacksdo_action()one call below WP_REST_Attachments_Controller::upload_from_data()

Further down the call graph this can also reach transient, cache, option, serialize and query. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 11 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_REST_Attachments_Controller::upload_from_data() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always14–18__()
!empty($data) && !empty($headers) && empty($filename)26::get_filename_from_disposition(), __()
!empty($data) && !empty($headers) && !empty($filename) && $expected !== false41::get_filename_from_disposition(), array_shift(), md5(), __()
!empty($data) && !empty($filename)46::get_filename_from_disposition(), array_shift(), wp_tempnam(), fopen(), __()
!empty($data) && !empty($filename) && is_wp_error()58::get_filename_from_disposition(), array_shift(), wp_tempnam(), fopen(), fwrite(), fclose(), ::check_upload_size(), is_wp_error()
!empty($data) && !empty($headers) && !empty($filename) && $expected === false59::get_filename_from_disposition(), array_shift(), md5(), array_shift(), wp_tempnam(), fopen(), __()
!empty($data) && !empty($filename) && !is_wp_error() && !isset($sideloaded)67::get_filename_from_disposition(), array_shift(), wp_tempnam(), fopen(), fwrite(), fclose(), ::check_upload_size(), is_wp_error(), wp_handle_sideload()
!empty($data) && !empty($headers) && !empty($filename) && $expected === false && is_wp_error()71::get_filename_from_disposition(), array_shift(), md5(), array_shift(), wp_tempnam(), fopen(), fwrite(), fclose(), ::check_upload_size(), is_wp_error()
!empty($data) && !empty($filename) && !is_wp_error() && isset($sideloaded)79::get_filename_from_disposition(), array_shift(), wp_tempnam(), fopen(), fwrite(), fclose(), ::check_upload_size(), is_wp_error(), wp_handle_sideload(), unlink(), error()
!empty($data) && !empty($headers) && !empty($filename) && $expected === false && !is_wp_error() && !isset($sideloaded)80::get_filename_from_disposition(), array_shift(), md5(), array_shift(), wp_tempnam(), fopen(), fwrite(), fclose(), ::check_upload_size(), is_wp_error(), wp_handle_sideload()
!empty($data) && !empty($headers) && !empty($filename) && $expected === false && !is_wp_error() && isset($sideloaded)92::get_filename_from_disposition(), array_shift(), md5(), array_shift(), wp_tempnam(), fopen(), fwrite(), fclose(), ::check_upload_size(), is_wp_error(), wp_handle_sideload(), unlink(), error()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev14814–929
8.514814–929
8.414814–9292 fewer instructions than PHP 8.3
8.315014–949
8.215014–949
8.115014–949
7.415014–949

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 7

Used by · 1

Source code

	protected function upload_from_data( $data, $headers, $time = null ) {		if ( empty( $data ) ) {			return new WP_Error(				'rest_upload_no_data',				__( 'No data supplied.' ),				array( 'status' => 400 )			);		} 		if ( empty( $headers['content_type'] ) ) {			return new WP_Error(				'rest_upload_no_content_type',				__( 'No Content-Type supplied.' ),				array( 'status' => 400 )			);		} 		if ( empty( $headers['content_disposition'] ) ) {			return new WP_Error(				'rest_upload_no_content_disposition',				__( 'No Content-Disposition supplied.' ),				array( 'status' => 400 )			);		} 		$filename = self::get_filename_from_disposition( $headers['content_disposition'] ); 		if ( empty( $filename ) ) {			return new WP_Error(				'rest_upload_invalid_disposition',				__( 'Invalid Content-Disposition supplied. Content-Disposition needs to be formatted as `attachment; filename="image.png"` or similar.' ),				array( 'status' => 400 )			);		} 		if ( ! empty( $headers['content_md5'] ) ) {			$content_md5 = array_shift( $headers['content_md5'] );			$expected    = trim( $content_md5 );			$actual      = md5( $data ); 			if ( $expected !== $actual ) {				return new WP_Error(					'rest_upload_hash_mismatch',					__( 'Content hash did not match expected.' ),					array( 'status' => 412 )				);			}		} 		// Get the content-type.		$type = array_shift( $headers['content_type'] ); 		// Include filesystem functions to get access to wp_tempnam() and wp_handle_sideload().		require_once ABSPATH . 'wp-admin/includes/file.php'; 		// Save the file.		$tmpfname = wp_tempnam( $filename ); 		$fp = fopen( $tmpfname, 'w+' ); 		if ( ! $fp ) {			return new WP_Error(				'rest_upload_file_error',				__( 'Could not open file handle.' ),				array( 'status' => 500 )			);		} 		fwrite( $fp, $data );		fclose( $fp ); 		// Now, sideload it in.		$file_data = array(			'error'    => null,			'tmp_name' => $tmpfname,			'name'     => $filename,			'type'     => $type,		); 		$size_check = self::check_upload_size( $file_data );

Changelog

Introduced in 4.7.0. One change between 6.7.7 and 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

7.1.0
Return type changed from array|WP_Error to array{.verified against source
6.6.0
Added the $time parameter.from the docblock
4.7.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 tag, from src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.