WP_REST_Attachments_Controller::upload_from_data()protectedWP 4.7.0

Handles an upload via raw POST data.

Method of the class: WP_REST_Attachments_Controller{}

No Hooks.

Return

Array|WP_Error. Data from wp_handle_sideload().

Usage

// protected - for code of main (parent) or child class
$result = $this->upload_from_data( $data, $headers );
$data(string) (required)
Supplied file data.
$headers(array) (required)
HTTP headers from the request.

Changelog

Since 4.7.0 Introduced.

WP_REST_Attachments_Controller::upload_from_data() code WP 6.4.3

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;
}