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.
Hooks from the function
Return
Array
. Information about the newly-uploaded file.
Usage
wp_upload_bits( $name, $deprecated, $bits, $time );
- $name(string) (required)
- Filename.
- $deprecated(null|string) (required)
- Never used. Set to null.
- $bits(string) (required)
- File content
- $time(string|null)
- Time formatted in 'yyyy/mm'.
Default: null
Examples
#1 How to upload file to a custom directory in WordPress
This example uploads a PDF document in the wp-content/uploads/customDirectory
folder:
<form action="" method="post" enctype="multipart/form-data"> Select image to upload: <input type="file" name="fileToUpload" id="fileToUpload"> <br> <input type="submit" value="Upload Image" name="submittheform"> </form>
global $wp_filesystem; WP_Filesystem(); $content_directory = $wp_filesystem->wp_content_dir() . 'uploads'; $custom_dirname = 'CustomDirectory'; $wp_filesystem->mkdir( "$content_directory/$custom_dirname" ); $target_dir_location = "$content_directory/$custom_dirname"; if ( isset( $_POST['submittheform'] ) && isset( $_FILES['fileToUpload'] ) ) { $name_file = $_FILES['fileToUpload']['name']; $tmp_name = $_FILES['fileToUpload']['tmp_name']; $cont = file_get_contents( $tmp_name ); $upload = wp_upload_bits( $name_file, null, $cont ); // let's check error if( $upload['error'] ){ echo 'The post caused an error: '. $upload['error']; } else { $move = move_uploaded_file( $upload['file'], "$target_dir_location/$name_file" ); if ( $move ) { echo "File was successfully uploaded"; } else { echo "The file was not uploaded"; } } }
#2 Demo
This example shows how to create a file in the download folder. Let's create an image. Let's take the content of an external picture with file_get_contents() and put it in the download folder of our site:
$cont = file_get_contents('http://example.com/some_img.png'); $new_file_name = 'uploaded.png'; $upload = wp_upload_bits( $new_file_name, null, $cont ); // let's check if the post if( $upload['error'] ) echo 'The post caused an error: '. $upload['error']; else echo 'The post is successful! File path: '. $upload['file'] .'; File URL: '. $upload['url'];
In the example we see that the $upload array looks like this:
Array ( [file] => /home/user/site/public_html/wp-content/2013/03/uploaded.png [url] => http://example.com/wp-content/uploads/2013/03/uploaded.png [type] => image/png [error] => )
Now, for example, we can display the downloaded picture using the url:
<img src="<?php echo $upload['url'] ?>" />
#3 Creating a file from a form
This example shows how to create a file passed in a form. It is assumed that the request is made from a form with field1
:
$upload = wp_upload_bits( $_FILES["field1"]["name"], null, file_get_contents( $_FILES["field1"]["tmp_name"] ) );
This function tries to save a copy of the uploaded file to the WordPress upload directory. It also performs a security function by checking the file (file type, size, etc.) and returns an error if any of the parameters are not allowed. After uploading, it is advisable to delete the temporary file.
Changelog
Since 2.0.0 | Introduced. |