wp_handle_upload( array $file, array|bool $overrides = false, string $time = null )

Wrapper for _wp_handle_upload().


Description Description

Passes the ‘wp_handle_upload’ action.

See also See also


Top ↑

Parameters Parameters

$file

(array) (Required) Reference to a single element of $_FILES. Call the function once for each uploaded file.

$overrides

(array|bool) (Optional) An associative array of names=>values to override default variables.

Default value: false

$time

(string) (Optional) Time formatted in 'yyyy/mm'.

Default value: null


Top ↑

Return Return

(array) On success, returns an associative array of file attributes. On failure, returns $overrides['upload_error_handler'](&$file, $message ) or array( 'error'=>$message ).


Top ↑

Source Source

File: wp-admin/includes/file.php

function wp_handle_upload( &$file, $overrides = false, $time = null ) {
	/*
	 *  $_POST['action'] must be set and its value must equal $overrides['action']
	 *  or this:
	 */
	$action = 'wp_handle_upload';
	if ( isset( $overrides['action'] ) ) {
		$action = $overrides['action'];
	}

	return _wp_handle_upload( $file, $overrides, $time, $action );
}


Top ↑

Changelog Changelog

Changelog
Version Description
2.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Codex

    Example

    if ( ! function_exists( 'wp_handle_upload' ) ) {
        require_once( ABSPATH . 'wp-admin/includes/file.php' );
    }
    
    $uploadedfile = $_FILES['file'];
    
    $upload_overrides = array(
    	'test_form' => false
    );
    
    $movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
    
    if ( $movefile && ! isset( $movefile['error'] ) ) {
        echo __( 'File is valid, and was successfully uploaded.', 'textdomain' ) . "\n";
        var_dump( $movefile );
    } else {
        /*
         * Error generated by _wp_handle_upload()
         * @see _wp_handle_upload() in wp-admin/includes/file.php
         */
        echo $movefile['error'];
    }
    
  2. Skip to note 2 content
    Contributed by Mohit Mishra
    if ( ! function_exists( 'wp_handle_upload' ) ) {
    	require_once( ABSPATH . 'wp-admin/includes/file.php' );
    }
    // for multiple file upload.
    $upload_overrides = array( 'test_form' => false );
    $files = $_FILES['file'];
    foreach ( $files['name'] as $key => $value ) {
    	if ( $files['name'][ $key ] ) {
    		$file = array(
    			'name' => $files['name'][ $key ],
    			'type' => $files['type'][ $key ],
    			'tmp_name' => $files['tmp_name'][ $key ],
    			'error' => $files['error'][ $key ],
    			'size' => $files['size'][ $key ]
    		);
    
    		$movefile = wp_handle_upload( $file, $upload_overrides );
    	}
    }
    }

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