WP_REST_Attachments_Controller::upload_from_file( array $files, 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:1400
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
$filesarray- Data from the
$_FILESsuperglobal. $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_upload().
Performance profile
How much work a call to WP_REST_Attachments_Controller::upload_from_file() 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
- Trivial
- Scaling
- Constant
- Instructions
- 14–60
- Plugin surface
- None
- Called by
- 1
Touches nothing outside its own arguments.
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 80.
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
- hookthird-party callbacks
do_action()one call below WP_REST_Attachments_Controller::upload_from_file()
Further down the call graph this can also reach transient, option, cache, query and serialize. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 6 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_file() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
empty($files) | 14 | __() |
!empty($files) && empty($headers) && is_wp_error() | 21–25 | ::check_upload_size(), is_wp_error() |
!empty($files) && !empty($headers) && $expected !== false | 31 | array_shift(), md5_file(), __() |
!empty($files) && empty($headers) && !is_wp_error() | 34–45 | ::check_upload_size(), is_wp_error(), wp_handle_upload() |
!empty($files) && !empty($headers) && $expected === false && is_wp_error() | 36–40 | array_shift(), md5_file(), ::check_upload_size(), is_wp_error() |
!empty($files) && !empty($headers) && $expected === false && !is_wp_error() | 49–60 | array_shift(), md5_file(), ::check_upload_size(), is_wp_error(), wp_handle_upload() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 80 | 14–60 | 7 | |
| 8.5 | 80 | 14–60 | 7 | |
| 8.4 | 80 | 14–60 | 7 | 2 fewer instructions than PHP 8.3 |
| 8.3 | 82 | 14–62 | 7 | |
| 8.2 | 82 | 14–62 | 7 | |
| 8.1 | 82 | 14–62 | 7 | |
| 7.4 | 82 | 14–62 | 7 |
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 · 5
- __()Retrieves the translation of $text.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- wp_handle_upload()Wrapper for _wp_handle_upload().
- WP_Error::__construct()Initializes the error.
- 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_file( $files, $headers, $time = null ) { if ( empty( $files ) ) { return new WP_Error( 'rest_upload_no_data', __( 'No data supplied.' ), array( 'status' => 400 ) ); } // Verify hash, if given. if ( ! empty( $headers['content_md5'] ) ) { $content_md5 = array_shift( $headers['content_md5'] ); $expected = trim( $content_md5 ); $actual = md5_file( $files['file']['tmp_name'] ); if ( $expected !== $actual ) { return new WP_Error( 'rest_upload_hash_mismatch', __( 'Content hash did not match expected.' ), array( 'status' => 412 ) ); } } // Pass off to WP to handle the actual upload. $overrides = array( 'test_form' => false, ); // Bypasses is_uploaded_file() when running unit tests. if ( defined( 'DIR_TESTDATA' ) && DIR_TESTDATA ) { $overrides['action'] = 'wp_handle_mock_upload'; } $size_check = self::check_upload_size( $files['file'] ); if ( is_wp_error( $size_check ) ) { return $size_check; } // Include filesystem functions to get access to wp_handle_upload(). require_once ABSPATH . 'wp-admin/includes/file.php'; $file = wp_handle_upload( $files['file'], $overrides, $time ); if ( isset( $file['error'] ) ) { return new WP_Error( 'rest_upload_unknown_error', $file['error'], array( 'status' => 500 ) ); } return $file; }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.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.