ChronoForms v8 - Upload path / how to create directory based of form values?

How to create a dynamic upload directory based on form values in ChronoForms v8.

Overview

The upload action does not automatically create directories, and using form data in the path requires proper PHP syntax.
Use a Pre Submit action with PHP code to create the directory using the form field values before the file upload processes.

Answered
ChronoForms v8
st studiopetrov 10 Oct, 2024

Hi,Can anyone explain in detail how I can set up a file upload so it can create a folder with other fields data in its name... in ChronoForms v8?For example: When John Doe uploads the file john.jpg, that file should go to .../images/uploads/{data:name}_{data:date}/john.jpg*(name and date are Field Names)

I have attached my current settings, but upload images directly to images/uploads/ without creating the subfolder.

Thanks in advance...

Screenshot 2024-10-10 at 11-54-07 ChronoForms 8 - International World Dance Passport - Administration.png
Max_admin Max_admin 15 Oct, 2024
Answer

The upload directory must exist before the file is saved there, it will not try to create it first, you can do this yourself using a PHP

You can use this code to create the folder:

if (!file_exists('path/to/directory')) {
    mkdir('path/to/directory', 0777, true);
}

But for this to work, you need to use the new "Pre Submit" action, this will allow the code above to run before the file field upload is processed, here is how it should be done:

ChronoForms v8 - Upload path / how to create directory based of form values? image 1

Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
st studiopetrov 17 Oct, 2024

Ok, and how can I use form field values for 'path/to/directory' in this PHP script?

Is this correct for example: 

if (!file_exists('path/to/directory/{data:name}_{data:surname}')) {
    mkdir('path/to/directory', 0777, true);
}

And for the Upload directory

'full/path/images/uploads/{data:name}_{data:surname}' ?

Max_admin Max_admin 17 Oct, 2024

no, in PHP you need to use $this->data("field_name") to get the data value, so the code should be:

'path/to/directory/'.$this->data("name").'_'.$this->data("surname")

by the way, the "pre submit" action is available in the next update, which is supposed to be published in the next few days

Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
This topic is locked and no more replies can be posted.