TutorialUnique Path for Every Upload Gravity Forms

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;
   }
  1. your-reference: you can fill in anything you like here. Every folder will start with this text.
  2. $path_info[“path”]: fill in the full physical path to the upload folder and use the created $uniqueid variable to create unique folder names.
  3. $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.

Read more tutorials about Rocketgenius, Inc.

Rocketgenius builds advanced software solutions. Rocketgenius is the team behind Gravity Forms. Visit GravityForms.com
All tutorials Rocketgenius, Inc.