_wp_handle_upload() – Handles PHP uploads in WordPress.

You appear to be a bot. Output may be restricted

Description

Handles PHP uploads in WordPress.

Sanitizes file names, checks extensions for mime type, and moves the file to the appropriate directory within the uploads directory.

Usage

$array = _wp_handle_upload( $file, $overrides, $time, $action );

Parameters

$file
( array ) required – { Reference to a single element from `$_FILES`. Call the function once for each uploaded file.
$name
( string ) required – The original name of the file on the client machine.
$type
( string ) required – The mime type of the file, if the browser provided this information.
$tmp_name
( string ) required – The temporary filename of the file in which the uploaded file was stored on the server.
$size
( int ) required – The size, in bytes, of the uploaded file.
$error
( int ) required – The error code associated with this file upload. }
$overrides
( array|false ) required – { An array of override parameters for this file, or boolean false if none are provided.
$upload_error_handler
( callable ) required – Function to call when there is an error during the upload process.
$unique_filename_callback
( callable ) required – Function to call when determining a unique file name for the file.
$upload_error_strings
( string[] ) required – The strings that describe the error indicated in `$_FILES[{form field}]['error']`.
$test_form
( bool ) required – Whether to test that the $_POST['action'] parameter is as expected.
$test_size
( bool ) required – Whether to test that the file size is greater than zero bytes.
$test_type
( bool ) required – Whether to test that the mime type of the file is as expected.
$mimes
( string[] ) required – Array of allowed mime types keyed by their file extension regex. }
$time
( string ) required – Time formatted in 'yyyy/mm'.
$action
( string ) required – Expected value for `$_POST['action']`.
$file
( string ) required – Filename of the newly-uploaded file.
$url
( string ) required – URL of the newly-uploaded file.
$type
( string ) required – Mime type of the newly-uploaded file. }

Returns

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

Source

File name: wordpress/wp-admin/includes/file.php
Lines:

201 to 274 of 274
   *     @type string $type     The mime type of the file, if the browser provided this information.
   *     @type string $tmp_name The temporary filename of the file in which the uploaded file was stored on the server.
   *     @type int    $size     The size, in bytes, of the uploaded file.
   *     @type int    $error    The error code associated with this file upload.
   * }
   * @param string   $new_file      Filename of the newly-uploaded file.
   * @param string   $type          Mime type of the newly-uploaded file.
   */
  $move_new_file = apply_filters( 'pre_move_uploaded_file', null, $file, $new_file, $type );

  if ( null === $move_new_file ) {
    if ( 'wp_handle_upload' === $action ) {
      $move_new_file = @move_uploaded_file( $file['tmp_name'], $new_file );
    } else {
      // Use copy and unlink because rename breaks streams.
      // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
      $move_new_file = @copy( $file['tmp_name'], $new_file );
      unlink( $file['tmp_name'] );
    }

    if ( false === $move_new_file ) {
      if ( 0 === strpos( $uploads['basedir'], ABSPATH ) ) {
        $error_path = str_replace( ABSPATH, '', $uploads['basedir'] ) . $uploads['subdir'];
      } else {
        $error_path = basename( $uploads['basedir'] ) . $uploads['subdir'];
      }

      return $upload_error_handler(
        $file,
        sprintf(
          /* translators: %s: Destination file path. */
          __( 'The uploaded file could not be moved to %s.' ),
          $error_path
        )
      );
    }
  }

  // Set correct file permissions.
  $stat  = stat( dirname( $new_file ) );
  $perms = $stat['mode'] & 0000666;
  chmod( $new_file, $perms );

  // Compute the URL.
  $url = $uploads['url'] . "/$filename";

  if ( is_multisite() ) {
    clean_dirsize_cache( $new_file );
  }

  
/**
 * Filters the data array for the uploaded file.
 *
 * @since 2.1.0
 *
 * @param array  $upload {
 *     Array of upload data.
 *
 *     @type string $file Filename of the newly-uploaded file.
 *     @type string $url  URL of the newly-uploaded file.
 *     @type string $type Mime type of the newly-uploaded file.
 * }
 * @param string $context The type of upload action. Values include 'upload' or 'sideload'.
 */
  return apply_filters(
    'wp_handle_upload',
    array(
      'file' => $new_file,
      'url'  => $url,
      'type' => $type,
    ),
    'wp_handle_sideload' === $action ? 'sideload' : 'upload'
  );
}
 

 View on GitHub View on Trac