Recently, I was working on a project where we wanted to upload images in the custom folder. We don’t want to use the default WordPress uploads structure where all your images get stored in a year-month format. Instead, we wanted to create our own folder inside the ‘uploads’ directory and store images in it. It requires me to write a program that can upload files through the HTML form.
In this article, we study how to upload files programmatically in WordPress. I will show you two ways for performing this task. One is through the built-in wp_upload_bits() function and another is using the move_uploaded_file() method.
Upload Files Programmatically In WordPress
To get started, create a simple HTML form with file input and submit button. You can place this form on your page or post.
<h2>Upload a File</h2>
<form method="post" enctype="multipart/form-data">
<input type="file" name="file" required />
<input type="submit" name="upload_file" value="Upload" />
</form>
Upload file using wp_upload_bits
Using wp_upload_bits()
function, your file will store inside the uploads directory. You will find your file inside the current year month folder(for ex. wp-content/uploads/2020->03->your_file). This function will not move a browsed file to the upload folder. Instead, it will create a new file with the content of the files. The use of this method is simple. In your functions.php
file, add the code as follows.
<?php
function fn_upload_file() {
if ( isset($_POST['upload_file']) ) {
$upload = wp_upload_bits($_FILES['file']['name'], null, $_FILES['file']['tmp_name']);
// save into database $upload['url]
}
}
add_action('init', 'fn_upload_file');
If you print the $upload
variable, you will get an array that contains the directory and URL path of the uploaded file. The user can save the URL path in the database using $upload['url]
.
Upload file in the Custom Directory
As mentioned above, using wp_upload_bits()
method your files will get stored using the default WordPress folder hierarchy. But what if you want to store files inside your own directory?
In that case, you need to create your custom folder in the ‘uploads’ directory and move your file into that folder. Let’s say you want to store your files inside the ‘uploads/product-images’ directory. In the code below, we will create a ‘product-images’ folder runtime and store files inside it. This code also you can write in the functions.php
file.
function fn_upload_file() {
if ( isset($_POST['upload_file']) ) {
$upload_dir = wp_upload_dir();
if ( ! empty( $upload_dir['basedir'] ) ) {
$user_dirname = $upload_dir['basedir'].'/product-images';
if ( ! file_exists( $user_dirname ) ) {
wp_mkdir_p( $user_dirname );
}
$filename = wp_unique_filename( $user_dirname, $_FILES['file']['name'] );
move_uploaded_file($_FILES['file']['tmp_name'], $user_dirname .'/'. $filename);
// save into database $upload_dir['baseurl'].'/product-images/'.$filename;
}
}
}
add_action('init', 'fn_upload_file');
Here, I am using the wp_unique_filename() method which gives the unique filename for the given directory.
That’s it! It’s all about how to upload files programmatically in WordPress. Are you using any other method for a similar task? Please share in the comment section below.
Related Articles
- How To Use wp_get_image_editor Method To Resize Your Images In WordPress
- How To Register Custom Image Sizes And Resize Existing Images In WordPress
- How To Set Featured Image Programmatically In WordPress
If you liked this article, then please subscribe to our YouTube Channel for video tutorials.