WP_REST_Attachments_Controller::upload_from_file( array $files, array $headers, string|null $time = null ): array|WP_Error

In this article

Handles an upload via multipart/form-data ($_FILES).

Parameters

$filesarrayrequired
Data from the $_FILES superglobal.
$headersarrayrequired
HTTP headers from the request.
$timestring|nulloptional
Time formatted in 'yyyy/mm'.

Default:null

Return

array|WP_Error Data from wp_handle_upload() .

Source

	$params['status']['items']['enum'] = array( 'inherit', 'private', 'trash' );
	$media_types                       = $this->get_media_types();

	$params['media_type'] = array(
		'default'     => null,
		'description' => __( 'Limit result set to attachments of a particular media type.' ),
		'type'        => 'string',
		'enum'        => array_keys( $media_types ),
	);

	$params['mime_type'] = array(
		'default'     => null,
		'description' => __( 'Limit result set to attachments of a particular MIME type.' ),
		'type'        => 'string',
	);

	return $params;
}

/**
 * Handles an upload via multipart/form-data ($_FILES).
 *
 * @since 4.7.0
 * @since 6.6.0 Added the `$time` parameter.
 *
 * @param array       $files   Data from the `$_FILES` superglobal.
 * @param array       $headers HTTP headers from the request.
 * @param string|null $time    Optional. Time formatted in 'yyyy/mm'. Default null.
 * @return array|WP_Error Data from wp_handle_upload().
 */
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 )
			);
		}
	}

Changelog

VersionDescription
6.6.0Added the $time parameter.
4.7.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.