Normally uploads in a Gravity Form are stored in the same folder. This is a problem when uploaded files are privacy sensitive and you want to share the links to files with people who filled in a form. They know the way to the folder, so they can get access to files from others (merely by guessing). Thankfully, it is possible to create unique folders for each upload.
How to save user uploads in a Gravity Form to separate folders?
Normally all file uploads via one Gravity Form are placed in a folder like:
wp-content/uploads/gravity_forms/1-b41819ae08fb08e6d3825745519c5748/2014/08
To store every upload into it’s own folder you can add the following code in your functions.php in your template folder:
add_filter("gform_upload_path", "change_upload_path", 10, 2);
function change_upload_path($path_info, $form_id){
$uniqueid = uniqid('your-reference', true);
$path_info["path"] = "/home/gravitywp/domains/gravitywp.com/public_html/uploads/$uniqueid/";
$path_info["url"] = "https://gravitywp.com/uploads/$uniqueid/"; return $path_info;
}
- your-reference: you can fill in anything you like here. Every folder will start with this text.
- $path_info[“path”]: fill in the full physical path to the upload folder and use the created $uniqueid variable to create unique folder names.
- $path_info[“url”]: fill in the full URL to the upload folder.
When you now upload a file in a form it will now generate an unique folder for each upload. For example:
https://gravitywp.com/uploads/your-reference554338385546f2374182415/example-file.pdf
Apache folder problem
When you create a folder for every file that is uploaded in Gravity Forms on your site, it’s possible (especially when you have a lot of people uploading files in your forms) that you create too many folders for Apache to handle. At a certain moment, Gravity Forms won’t be able to create a new folder for the upload and the upload will fail. There is quite a simple solution for this, because we know now how to create a filter for our uploads. Just add the following to your functions.php (instead of the above code):
add_filter("gform_upload_path", "change_upload_path", 10, 2);
function change_upload_path($path_info, $form_id){
$uniquefolder = rand(1,9999);
$uniqueid = uniqid('your-reference', true);
$path_info["path"] = "/home/gravitywp/domains/gravitywp.com/public_html/uploads/$uniquefolder/$uniqueid/";
$path_info["url"] = "https://gravitywp.com/uploads/$uniquefolder/$uniqueid/"; return $path_info;
}
The new variable added is $uniquefolder.
$uniquefolder = rand(1,9999);
This will pick a random number between 1 and 9999 and create this folder for us. If the folder exist, it will place our unique folder inside this ‘number folder’. This way you multiplied the number of available slots by 10.000. You have a very successful website when you beat that.
Use the Form ID as folder name for uploads
A nice extra would be to add the Form ID of the form people upload documents in to the upload path. This is quite easy and also makes it less likely that you run out of available space on your server. You just add a third variable that is already available, because you are using it in combination with Gravity Forms: $form_id (look also to the arguments passed in the function):
add_filter("gform_upload_path", "change_upload_path", 10, 2);
function change_upload_path($path_info, $form_id){
$uniquefolder = rand(1,9999);
$uniqueid = uniqid('your-reference', true);
$path_info["path"] = "/home/gravitywp/domains/gravitywp.com/public_html/uploads/$form_id/$uniquefolder/$uniqueid/";
$path_info["url"] = "https://gravitywp.com/uploads/$form_id/$uniquefolder/$uniqueid/"; return $path_info;
}
As you can see now you have three levels of folders you create for every upload:
.../uploads/$form_id/$uniquefolder/$uniqueid/"; .../uploads/$form_id/$uniquefolder/$uniqueid/"; return $path_info;
Use relative $path_info
There is a simple way to use WordPress core functionality to find your current upload path and url. Just use this code instead of the hard coded path and url:
add_filter("gform_upload_path", "change_upload_path", 10, 2);
function change_upload_path($path_info, $form_id) {
$uniqueid = uniqid('your-reference', true);
$path_info["path"] = WP_CONTENT_DIR . "/files/";
$path_info["url"] = WP_CONTENT_URL . "/files/";
return $path_info;
}
This way uploads will be placed automatically in https://yourdomain.io/files/
Using entry data in upload path Gravity Forms
You can also use data from fields in the entry to create a unique upload path or structure your uploads. You can access the data with:
rgpost( 'input_3'
Where the field_id is 3, you use input_3. If you want data from an advanced field with multiple inputs, you can use:
rgpost( 'input_3_2'
This way you can get data from a specific subfield in for example the name or address field.
add_filter("gform_upload_path", "change_upload_path", 10, 2);
function change_upload_path($path_info, $form_id){
$category = sanitize_title(rgpost( 'input_3' ));
$title = sanitize_title(rgpost( 'input_2' ));
$path_info["path"] = WP_CONTENT_DIR . "/files/" . $category . "/" . $title . "/";
$path_info["url"] = WP_CONTENT_URL . "/files/" . $category . "/" . $title . "/";
return $path_info;
}
Be aware to sanitize the input you use for the upload path / url. We advise to use the sanitize_title function from WordPress:
sanitize_title(rgpost( 'input_3' ))
This way you’ll be sure the value being used is suited to use as folder name.
Our Premium add-ons for Gravity Forms
All Entries
All your new Gravity Forms entries in one central place. Stop navigating between multiple forms to find new entries. Get immediate oversight and full control with a powerful, unified dashboard.
n8n Connector
Connect Gravity Forms to n8n and automate your workflows with secure, flexible, and powerful webhooks. Go beyond simple notifications and build advanced, two-way automations.
Entry to Database
Integrates Gravity Forms with internal or external databases, offering flexible mapping of form fields to database columns and real-time synchronization between entries and database rows.
List Text
Add features like textarea, placeholder and custom validation to a column or multiple columns in a Gravity Forms List Field.
Read tutorials about
Show Previous Gravity Forms Submissions Using Advanced Merge Tags
This step-by-step tutorial shows how to use GravityWP Advanced Merge Tags to count previous Gravity Forms submissions and calculate total submitted value based on a matching email address.
Gravity Forms LeadConnector Integration with OAuth 2.0
This documentation-based tutorial explains how to connect Gravity Forms to LeadConnector/HighLevel with OAuth 2.0, search contacts by email, and map returned CRM values back into form fields using GravityWP API Connector.
Gravity Forms Zoho CRM Integration: How to Auto-Populate Form Fields by Email
Learn how to auto-populate Gravity Forms from Zoho CRM using GravityWP API Connector. This tutorial shows how to search Zoho Contacts by email and fill form fields with the returned data.
How to Create a Public Submission Business Directory in WordPress (Gravity Forms + GravityView)
Build a moderated business directory where users submit listings through a form, admins approve entries, and approved businesses appear in a searchable GravityView directory.