Dynamically control the upload destination

The RSForm!Pro file upload field only provides a static destination path for your uploads. All the uploads made through your form will arrive in this location. In case you would like to have separate folders for your submitting users, you can try one of the following examples.

1. Using the component specific PHP Scripting area

In our first example we will be using the PHP Scripts > Scripts called on form process area to control the destination folder for the upload.

$customPath = $_POST['form']['field_name_here'];          // this will define the custom path for the upload. In this example we are using a value from a different form field.
$myFieldId = RSFormProHelper::getComponentId('upload_field_name');  // here we retrieve the id the upload field
$properties = &RSFormProHelper::getComponentProperties($myFieldId); // next we need to get the properties of our upload field
$properties['DESTINATION'] .= $customPath."/";            // we then add the custom path we generated earlier to our default destination path
if (!file_exists($properties['DESTINATION'])) {            // if the folder does not exist we create it with sufficient permissions
mkdir($properties['DESTINATION'], 0755, true);            
}

2. Using the File Prefix attribute of the upload field.

For this second example to work correctly you will need to make sure that the users submitting the form are already logged in since we will be using the id of the logged in user as our extra folder.

//<code>
return Joomla\CMS\Factory::getUser()->id . DIRECTORY_SEPARATOR;
//</code>

With this implementation the file prefix will be used to add the extra folder. The file prefix is used to ensure that no duplicates are generated in your upload folder, but in this case we will use it to change the destination folder, for example if your current destination folder is components/com_rsform/uploads, when uploading a file through your form the upload will be
components/com_rsform/uploads/file-prefix_your_upload_file_name.

Using the above code snippet the user id will be used to create an extra level, the destination becoming components/com_rsform/uploads/logged-in-user-id/, thus resulting in the upload being components/com_rsform/uploads/file-prefix/your_upload_file_name. Since each user will have a different id when logged in the folders for each submitter will be different.


One person found this article helpful.


Was this article helpful?

Yes No
Sorry about that