Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/wp upload bits


Create a file in the upload folder with given content.

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.


<?php wp_upload_bits$name$deprecated$bits$time ?>


(string) (required)
Default: None
(null) (required) Not used. Set to null.
Default: None
(mixed) (required) File content
Default: None
(string) (optional) Time formatted in 'yyyy/mm'.
Default: null

Return Values

The function returns an array with the following keys:

The file system path to the uploaded file (e.g. /var/www/wordpress/wp-content/uploads/2010/03/example.pdf)
The URL to the uploaded file (e.g. http://example.com/wp-content/uploads/2010/03/example.pdf)
The file type as given by wp_check_filetype()['type'] (e.g. image/jpeg )
When something goes wrong with the upload, the error message is stored in this key. Otherwise set to false


Here's a simple example assuming the request was made from a form with a file field called field1:

$upload = wp_upload_bits($_FILES["field1"]["name"], null, file_get_contents($_FILES["field1"]["tmp_name"]));

The function attempts to save a copy of the uploaded file to the upload directory set in the WordPress settings. It also performs security checks (file type, size, etc) and returns errors if any (see Return Values above). You might want to remove the tmp file after uploading.


Change Log

Since: 2.0.0

Source File

wp_upload_bits() is located in wp-includes/functions.php.


See also index of Function Reference and index of Template Tags.