How to Rename an Uploaded File

This guide walks you through the process of renaming an uploaded file by using a custom PHP script.


The Script

To rename the uploaded file, you’ll need to add a PHP script in Form Properties > PHP Scripts - Pre-Processing > Script called before form is validated area. For example, the code below renames the uploaded file using a custom name, and the extension is changed to jpg:

$fieldName = 'FileUpload';
if (isset($_FILES, $_FILES['form'], $_FILES['form']['name'][$fieldName]))
{
  $_FILES['form']['name'][$fieldName] = 'newname.jpg';
}

Make sure to replace 'FileUpload' with your actual field name.

When you upload a file through a File Upload field created in RSForm!Pro, the component will automatically add a unique prefix to the file in order to help avoid duplicates. You can control the prefix of uploaded files as explained here.

 

The Result

When viewing or editing the form’s submissions, you will see that the custom filename defined in your script appears after the default file upload prefix.

 

Was this article helpful?

Yes No
Sorry about that