wp_upload_bits( string $name, null|string $deprecated, string $bits, string|null $time = null ): array
- Since
- 2.0.0
- Source
wp-includes/functions.php:2889
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- Never 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.
$filestringFilename of the newly-uploaded file.$urlstringURL of the uploaded file.$typestringFile type.$errorstring|falseError 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
- Scaling
- Constant
- Instructions
- 13–107
- Plugin surface
- 2 hooks
- Called by
- 2
Reads or writes the filesystem via fopen().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 158.
Third-party callbacks on 'wp_upload_bits', 'wp_handle_upload' run inside this call, and their cost is not bounded by anything here.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - filesystemfilesystem access
fopen()called directly - transienttransient
get_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.
| When | Instructions | Calls it makes |
|---|---|---|
empty($deprecated) && empty($name) | 13 | __() |
!empty($deprecated) && empty($name) | 17 | _deprecated_argument(), __() |
empty($deprecated) && !empty($name) && $wp_filetype | 22 | wp_check_filetype(), wp_upload_dir() |
empty($deprecated) && !empty($name) && !$wp_filetype && !current_user_can() | 23 | wp_check_filetype(), current_user_can(), __() |
empty($deprecated) && !empty($name) && !$wp_filetype && current_user_can() | 26 | wp_check_filetype(), current_user_can(), wp_upload_dir() |
!empty($deprecated) && !empty($name) && $wp_filetype | 26 | _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) | 34 | wp_check_filetype(), wp_upload_dir(), name() |
empty($deprecated) && !empty($name) && !$wp_filetype && current_user_can() && !is_array($upload_bits_error) | 38 | wp_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() | 63 | wp_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() | 67 | wp_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() | 67 | 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() | 67 | wp_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() | 71 | wp_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() | 71 | 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() | 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() | 96 | 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 && is_array($upload_bits_error) && wp_mkdir_p() && is_multisite() | 99 | 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() | 100 | 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 && 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() | 103 | 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() |
!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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 158 | 13–107 | 10 | |
| 8.5 | 158 | 13–107 | 10 | |
| 8.4 | 158 | 13–107 | 10 | 10 fewer instructions than PHP 8.3 |
| 8.3 | 168 | 13–111 | 10 | |
| 8.2 | 168 | 13–111 | 10 | |
| 8.1 | 168 | 13–111 | 10 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 169 | 13–111 | 10 |
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:
- apply_filters( wp_upload_bits )filterline 2919 (+30 into the body)
Filters whether to treat the upload bits as an error.
- apply_filters( wp_handle_upload )filterline 2977 (+88 into the body)
Filters the data array for the uploaded file.
Uses · 12
- _deprecated_argument()Marks a function argument as deprecated and inform when it has been used.
- __()Retrieves the translation of $text.
- wp_check_filetype()Retrieves the file type from the file name.
- current_user_can()Returns whether the current user has the specified capability.
- wp_upload_dir()Returns an array containing the current upload directory's path and URL.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- wp_unique_filename()Gets a filename that is sanitized and unique for the given directory.
- wp_mkdir_p()Recursive directory creation based on full path.
- str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
- wp_basename()i18n-friendly version of basename().
- is_multisite()Determines whether Multisite is enabled.
- clean_dirsize_cache()Cleans directory size cache used by recurse_dirsize().
Used by · 2
- wp_generate_attachment_metadata()Generates attachment meta data and create image sub-sizes for images.
- wp_xmlrpc_server::mw_newMediaObject()Uploads a file, following your settings.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 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.