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:1066
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
- Scaling
- Constant
- Instructions
- 14–92
- Plugin surface
- None
- Called by
- 1
Reads or writes the filesystem via fopen().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 148.
Nothing here hands control to plugin code.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- filesystemfilesystem access
fopen()called directly - hookthird-party callbacks
do_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 14–18 | __() |
!empty($data) && !empty($headers) && empty($filename) | 26 | ::get_filename_from_disposition(), __() |
!empty($data) && !empty($headers) && !empty($filename) && $expected !== false | 41 | ::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 === false | 59 | ::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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 148 | 14–92 | 9 | |
| 8.5 | 148 | 14–92 | 9 | |
| 8.4 | 148 | 14–92 | 9 | 2 fewer instructions than PHP 8.3 |
| 8.3 | 150 | 14–94 | 9 | |
| 8.2 | 150 | 14–94 | 9 | |
| 8.1 | 150 | 14–94 | 9 | |
| 7.4 | 150 | 14–94 | 9 |
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
- __()Retrieves the translation of $text.
- wp_tempnam()Returns a filename of a temporary unique file.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- wp_handle_sideload()Wrapper for _wp_handle_upload().
- WP_Error::__construct()Initializes the error.
- WP_REST_Attachments_Controller::get_filename_from_disposition()Parses filename from a Content-Disposition header value.
- WP_REST_Attachments_Controller::check_upload_size()Determine if uploaded file exceeds space quota on multisite.
Used by · 1
- WP_REST_Attachments_Controller::insert_attachment()Inserts the attachment post in the database. Does not update the attachment meta.
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.
Signature, return type and hooks compared across 5 parsed releases.
array|WP_Error to array{.verified against source$time parameter.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 6.7.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.