How to Define a Specific Directory For File Uploads

Overview

Would you like to define a specific directory to store your file uploads that are uploaded through the WPForms File Upload form field? With a small PHP snippet, you can easily define this directory that will hold all of your form uploads and we’ll walk you through each step.

Setup

To define your directory, you’ll need to copy this snippet to your site. You’ll need to know the server path of where you want to store these. These can be different depending on how the site is set up.

For example your root path could be ‘/home/mysitedomain.com/custom-dir’; or it could be ‘/home/sites/mysitedomain.com/custom-dir’; or even ‘/home/mysitedomain/public_html/custom-dir’;. If you’re unsure what your root path would be, check with your hosting provider.

/**
 * Define a specfic directory for files uploaded through the File Upload addon.
 *
 * @link  https://wpforms.com/developers/how-to-define-a-specific-directory-for-file-uploads/
 */

 function wpf_dev_upload_root( $path ) {

    //Define the path for your file uploads here
    $path = '/home/mysitedomain/public_html/custom-dir';  
       
    return $path;
      
}
add_filter( 'wpforms_upload_root', 'wpf_dev_upload_root', 10, 1 );

Now when files are uploaded through your forms, they will be stored in the custom-dir folder on your server.

Would you like to alter the timeout function on the Modern Upload Field? Check out our snippet on How to Change the Timeout on the Modern File Upload.

Filter Reference: wpforms_upload_root