Function: media_handle_upload

media_handle_upload( string $file_id, integer $post_id, array $post_data, array $overrides )

Save a file submitted from a POST request and create an attachment post for it.

Parameters

Name Type(s) Default Value Description
$file_id string

Index of the $_FILES array that the file was sent. Required.

$post_id integer

The post ID of a post to attach the media item to. Required, but can be set to 0, creating a media item that has no relationship to a post.

$post_data array array()

Overwrite some of the attachment. Optional.

$overrides array array('test_form' => false)

Override the wp_handle_upload() behavior. Optional.

Returns

integer | WP_Error

ID of the attachment or a WP_Error object on failure.

WordPress Developer Newsletter

Stay on top of the latest WordPress API changes, developer tool updates, security alerts and more.