wppaste
WordPress

wp_upload_bits( string $name, null|string $deprecated, string $bits, string|null $time = null ): array

Since
2.0.0
Source
wp-includes/functions.php:2924
Creates a file in the upload folder with given content.

Description

If there is an error, then the key 'error' will exist with the error message.
If success, then the key 'file' will have the unique file path, the 'url' key will have the link to the new file. and the 'error' key will be set to false.

This function will not move an uploaded file to the upload folder. It will create a new file with the content in $bits parameter. If you move the upload file, read the content of the uploaded file, and then you can give the filename and content to this function, which will add it to the upload folder.

The permissions will be set on the new file automatically by this function.

Compatibility

WordPress
since 2.0.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$namestring
Filename.
$deprecatednull|string
Not used. Set to null.
$bitsstring
File content
$timestring|nulloptional
Time formatted in 'yyyy/mm'. Default null.Default: null

Return value

array
Information about the newly-uploaded file.
  • $filestring

    Filename of the newly-uploaded file.
  • $urlstring

    URL of the uploaded file.
  • $typestring

    File type.
  • $errorstring|false

    Error message, if there has been an error.

Performance profile

How much work a call to wp_upload_bits() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reads or writes the filesystem via fopen().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
13–107

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 158.

Plugin surface
2 hooks

Third-party callbacks on 'wp_upload_bits', 'wp_handle_upload' run inside this call, and their cost is not bounded by anything here.

Called by
2

2 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • filesystemfilesystem accessfopen()called directly
  • transienttransientget_transient()one call below wp_upload_bits()

Further down the call graph this can also reach option, cache, query and serialize. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 32 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_upload_bits() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
empty($deprecated) && empty($name)13__()
!empty($deprecated) && empty($name)17_deprecated_argument(), __()
empty($deprecated) && !empty($name) && $wp_filetype22wp_check_filetype(), wp_upload_dir()
empty($deprecated) && !empty($name) && !$wp_filetype && !current_user_can()23wp_check_filetype(), current_user_can(), __()
empty($deprecated) && !empty($name) && !$wp_filetype && current_user_can()26wp_check_filetype(), current_user_can(), wp_upload_dir()
!empty($deprecated) && !empty($name) && $wp_filetype26_deprecated_argument(), wp_check_filetype(), wp_upload_dir()
!empty($deprecated) && !empty($name) && !$wp_filetype && !current_user_can()27_deprecated_argument(), wp_check_filetype(), current_user_can(), __()
!empty($deprecated) && !empty($name) && !$wp_filetype && current_user_can()30_deprecated_argument(), wp_check_filetype(), current_user_can(), wp_upload_dir()
empty($deprecated) && !empty($name) && $wp_filetype && !is_array($upload_bits_error)34wp_check_filetype(), wp_upload_dir(), name()
empty($deprecated) && !empty($name) && !$wp_filetype && current_user_can() && !is_array($upload_bits_error)38wp_check_filetype(), current_user_can(), wp_upload_dir(), name()
!empty($deprecated) && !empty($name) && $wp_filetype && !is_array($upload_bits_error)38_deprecated_argument(), wp_check_filetype(), wp_upload_dir(), name()
!empty($deprecated) && !empty($name) && !$wp_filetype && current_user_can() && !is_array($upload_bits_error)42_deprecated_argument(), wp_check_filetype(), current_user_can(), wp_upload_dir(), name()
20 further outcomes, up to 107 instructions
empty($deprecated) && !empty($name) && $wp_filetype && is_array($upload_bits_error) && wp_mkdir_p()63wp_check_filetype(), wp_upload_dir(), name(), wp_unique_filename(), wp_mkdir_p(), fopen(), __(), sprintf()
empty($deprecated) && !empty($name) && $wp_filetype && is_array($upload_bits_error) && !wp_mkdir_p()67wp_check_filetype(), wp_upload_dir(), name(), wp_unique_filename(), wp_mkdir_p(), __(), sprintf()
empty($deprecated) && !empty($name) && $wp_filetype && is_array($upload_bits_error) && !wp_mkdir_p()67wp_check_filetype(), wp_upload_dir(), name(), wp_unique_filename(), wp_mkdir_p(), wp_basename(), __(), sprintf()
empty($deprecated) && !empty($name) && !$wp_filetype && current_user_can() && is_array($upload_bits_error) && wp_mkdir_p()67wp_check_filetype(), current_user_can(), wp_upload_dir(), name(), wp_unique_filename(), wp_mkdir_p(), fopen(), __(), sprintf()
!empty($deprecated) && !empty($name) && $wp_filetype && is_array($upload_bits_error) && wp_mkdir_p()67_deprecated_argument(), wp_check_filetype(), wp_upload_dir(), name(), wp_unique_filename(), wp_mkdir_p(), fopen(), __(), sprintf()
empty($deprecated) && !empty($name) && !$wp_filetype && current_user_can() && is_array($upload_bits_error) && !wp_mkdir_p()71wp_check_filetype(), current_user_can(), wp_upload_dir(), name(), wp_unique_filename(), wp_mkdir_p(), __(), sprintf()
!empty($deprecated) && !empty($name) && $wp_filetype && is_array($upload_bits_error) && !wp_mkdir_p()71_deprecated_argument(), wp_check_filetype(), wp_upload_dir(), name(), wp_unique_filename(), wp_mkdir_p(), __(), sprintf()
empty($deprecated) && !empty($name) && !$wp_filetype && current_user_can() && is_array($upload_bits_error) && !wp_mkdir_p()71wp_check_filetype(), current_user_can(), wp_upload_dir(), name(), wp_unique_filename(), wp_mkdir_p(), wp_basename(), __(), sprintf()
!empty($deprecated) && !empty($name) && $wp_filetype && is_array($upload_bits_error) && !wp_mkdir_p()71_deprecated_argument(), wp_check_filetype(), wp_upload_dir(), name(), wp_unique_filename(), wp_mkdir_p(), wp_basename(), __(), sprintf()
!empty($deprecated) && !empty($name) && !$wp_filetype && current_user_can() && is_array($upload_bits_error) && wp_mkdir_p()71_deprecated_argument(), wp_check_filetype(), current_user_can(), wp_upload_dir(), name(), wp_unique_filename(), wp_mkdir_p(), fopen(), __(), sprintf()
!empty($deprecated) && !empty($name) && !$wp_filetype && current_user_can() && is_array($upload_bits_error) && !wp_mkdir_p()75_deprecated_argument(), wp_check_filetype(), current_user_can(), wp_upload_dir(), name(), wp_unique_filename(), wp_mkdir_p(), __(), sprintf()
!empty($deprecated) && !empty($name) && !$wp_filetype && current_user_can() && is_array($upload_bits_error) && !wp_mkdir_p()75_deprecated_argument(), wp_check_filetype(), current_user_can(), wp_upload_dir(), name(), wp_unique_filename(), wp_mkdir_p(), wp_basename(), __(), sprintf()
empty($deprecated) && !empty($name) && $wp_filetype && is_array($upload_bits_error) && wp_mkdir_p() && !is_multisite()96wp_check_filetype(), wp_upload_dir(), name(), wp_unique_filename(), wp_mkdir_p(), fopen(), fwrite(), fclose(), clearstatcache(), stat(), chmod(), clearstatcache(), is_multisite(), file()
empty($deprecated) && !empty($name) && $wp_filetype && is_array($upload_bits_error) && wp_mkdir_p() && is_multisite()99wp_check_filetype(), wp_upload_dir(), name(), wp_unique_filename(), wp_mkdir_p(), fopen(), fwrite(), fclose(), clearstatcache(), stat(), chmod(), clearstatcache(), is_multisite(), clean_dirsize_cache(), file()
empty($deprecated) && !empty($name) && !$wp_filetype && current_user_can() && is_array($upload_bits_error) && wp_mkdir_p() && !is_multisite()100wp_check_filetype(), current_user_can(), wp_upload_dir(), name(), wp_unique_filename(), wp_mkdir_p(), fopen(), fwrite(), fclose(), clearstatcache(), stat(), chmod(), clearstatcache(), is_multisite(), file()
!empty($deprecated) && !empty($name) && $wp_filetype && is_array($upload_bits_error) && wp_mkdir_p() && !is_multisite()100_deprecated_argument(), wp_check_filetype(), wp_upload_dir(), name(), wp_unique_filename(), wp_mkdir_p(), fopen(), fwrite(), fclose(), clearstatcache(), stat(), chmod(), clearstatcache(), is_multisite(), file()
empty($deprecated) && !empty($name) && !$wp_filetype && current_user_can() && is_array($upload_bits_error) && wp_mkdir_p() && is_multisite()103wp_check_filetype(), current_user_can(), wp_upload_dir(), name(), wp_unique_filename(), wp_mkdir_p(), fopen(), fwrite(), fclose(), clearstatcache(), stat(), chmod(), clearstatcache(), is_multisite(), clean_dirsize_cache(), file()
!empty($deprecated) && !empty($name) && $wp_filetype && is_array($upload_bits_error) && wp_mkdir_p() && is_multisite()103_deprecated_argument(), wp_check_filetype(), wp_upload_dir(), name(), wp_unique_filename(), wp_mkdir_p(), fopen(), fwrite(), fclose(), clearstatcache(), stat(), chmod(), clearstatcache(), is_multisite(), clean_dirsize_cache(), file()
!empty($deprecated) && !empty($name) && !$wp_filetype && current_user_can() && is_array($upload_bits_error) && wp_mkdir_p() && !is_multisite()104_deprecated_argument(), wp_check_filetype(), current_user_can(), wp_upload_dir(), name(), wp_unique_filename(), wp_mkdir_p(), fopen(), fwrite(), fclose(), clearstatcache(), stat(), chmod(), clearstatcache(), is_multisite(), file()
!empty($deprecated) && !empty($name) && !$wp_filetype && current_user_can() && is_array($upload_bits_error) && wp_mkdir_p() && is_multisite()107_deprecated_argument(), wp_check_filetype(), current_user_can(), wp_upload_dir(), name(), wp_unique_filename(), wp_mkdir_p(), fopen(), fwrite(), fclose(), clearstatcache(), stat(), chmod(), clearstatcache(), is_multisite(), clean_dirsize_cache(), file()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev15813–10710
8.515813–10710
8.415813–1071010 fewer instructions than PHP 8.3
8.316813–11110
8.216813–11110
8.116813–111101 fewer instruction than PHP 7.4
7.416913–11110

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 2

2 hooks fire while wp_upload_bits() runs, in this order:

  1. apply_filters( wp_upload_bits )filterline 2954 (+30 into the body)

    Filters whether to treat the upload bits as an error.

  2. apply_filters( wp_handle_upload )filterline 3012 (+88 into the body)

    Filters the data array for the uploaded file.

Uses · 12

Used by · 2

Source code

function wp_upload_bits( $name, $deprecated, $bits, $time = null ) {	if ( ! empty( $deprecated ) ) {		_deprecated_argument( __FUNCTION__, '2.0.0' );	} 	if ( empty( $name ) ) {		return array( 'error' => __( 'Empty filename' ) );	} 	$wp_filetype = wp_check_filetype( $name );	if ( ! $wp_filetype['ext'] && ! current_user_can( 'unfiltered_upload' ) ) {		return array( 'error' => __( 'Sorry, you are not allowed to upload this file type.' ) );	} 	$upload = wp_upload_dir( $time ); 	if ( false !== $upload['error'] ) {		return $upload;	} 	/**	 * Filters whether to treat the upload bits as an error.	 *	 * Returning a non-array from the filter will effectively short-circuit preparing the upload bits	 * and return that value instead. An error message should be returned as a string.	 *	 * @since 3.0.0	 *	 * @param array|string $upload_bits_error An array of upload bits data, or error message to return.	 */	$upload_bits_error = apply_filters(		'wp_upload_bits',		array(			'name' => $name,			'bits' => $bits,			'time' => $time,		)	);	if ( ! is_array( $upload_bits_error ) ) {		$upload['error'] = $upload_bits_error;		return $upload;	} 	$filename = wp_unique_filename( $upload['path'], $name ); 	$new_file = $upload['path'] . "/$filename";	if ( ! wp_mkdir_p( dirname( $new_file ) ) ) {		if ( str_starts_with( $upload['basedir'], ABSPATH ) ) {			$error_path = str_replace( ABSPATH, '', $upload['basedir'] ) . $upload['subdir'];		} else {			$error_path = wp_basename( $upload['basedir'] ) . $upload['subdir'];		} 		$message = sprintf(			/* translators: %s: Directory path. */			__( 'Unable to create directory %s. Is its parent directory writable by the server?' ),			$error_path		);		return array( 'error' => $message );	} 	$ifp = @fopen( $new_file, 'wb' );	if ( ! $ifp ) {		return array(			/* translators: %s: File name. */			'error' => sprintf( __( 'Could not write file %s' ), $new_file ),		);	} 	fwrite( $ifp, $bits );	fclose( $ifp );	clearstatcache(); 	// Set correct file permissions.	$stat  = @ stat( dirname( $new_file ) );	$perms = $stat['mode'] & 0007777;	$perms = $perms & 0000666;	chmod( $new_file, $perms );	clearstatcache();

Changelog

Introduced in 2.0.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/functions.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.