WP_REST_Attachments_Controller::upload_from_data( array $data, array $headers )
Handles an upload via raw POST data.
Parameters
- $data
-  (array) (Required) Supplied file data. 
- $headers
-  (array) (Required) HTTP headers from the request. 
Return
(array|WP_Error) Data from wp_handle_sideload().
Source
File: wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php
protected function upload_from_data( $data, $headers ) {
		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 );
		if ( is_wp_error( $size_check ) ) {
			return $size_check;
		}
		$overrides = array(
			'test_form' => false,
		);
		$sideloaded = wp_handle_sideload( $file_data, $overrides );
		if ( isset( $sideloaded['error'] ) ) {
			@unlink( $tmpfname );
			return new WP_Error(
				'rest_upload_sideload_error',
				$sideloaded['error'],
				array( 'status' => 500 )
			);
		}
		return $sideloaded;
	}  Changelog
| Version | Description | 
|---|---|
| 4.7.0 | Introduced. | 
    © 2003–2021 WordPress Foundation
Licensed under the GNU GPLv2+ License.
    https://developer.wordpress.org/reference/classes/wp_rest_attachments_controller/upload_from_data