Dynamically create upload directory using form input-SOLVED

gorangrooves 25 Aug, 2010
Hi there,
this is a variation on the question of "How to Create a Directory using $user and ChronoForms?" posted here:
http://www.chronoengine.com/forums.html?cont=posts&f=5&t=13805&hilit=on+submit+create+folder&start=15

What I would like to do:
I am creating a "send file" form. I want to have a registered user being able to select an "existing folder" name from a drop down menu, then specify a name of a "new folder" to be created within the "existing folder" by typing it into an input box. User would then select a file to be uploaded to the "new folder".
On submit, "new folder" would be created within an "existing folder" and the file would be uploaded into the "new folder".

Structure would be something like this:
MySite/uploads/existing_folder/new_folder/my_uploaded_file

E-mail would then be sent with the download links.

I followed the instructions on how to dynamically create a directory using $user, but I don't know how to implement the fields inputed in the form into the extra php code on submit.

I can imagine that this may come handy to many people.
Please note that I am not a programmer, but just figuring things out using logic πŸ™‚

Any help is MUCH APPRECIATED. Thanks in advance!
GreyHead 25 Aug, 2010
Hi gorangrooves ,

You are the first person to ask so far so I guess it's not that popular :-(

You can get a list of folders using the Joomla! JFolder code and create a drop-down list from that. The new folder name would be a simple text input and then you can assemble the parts using the code in the thread you linked to.

Bob
gorangrooves 25 Aug, 2010
Thanks Bob for such a quick reply,
I think the issue would be pretty popular, but people are just afraid to ask 🀣

I did create a drop down list with folder names (already created) before I posted the question. Not sure how I would implement your suggestion of utilizing JFolder within the form and have them display in drop down. (not a programmer unfortunately, although I am playing one) :?

I don't know how to get the extra 1 php code to insert those inputed fields from the form. In my case they are select_2 and text_5. Instead of the system creating a folder named "tony" within a folder named "guitars", it creates "text_5" folder within a folder "select_2" !
What is the correct way to specify to php code that I want select_2 to be turned into whatever text was chosen in the form and text_5 to be turned into user inputed text (like "tony").
I tried writing it like {select_2} and [select_2] and ['select_2'] and 'select_2' but neither worked.

A little bit of php wisdom is much appreciated.

Thanks heaps!
Goran
Max_admin 27 Aug, 2010
Hi Goran,

In order to get the value of some form field after the form is submitted, you need to use this formula:

JRequest::getVar('field_name');


replace field_name by "select_5" for example and you should get the value oft he selected option

I wish this helps!

Regards,
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
gorangrooves 27 Aug, 2010
Thanks to the help of ChronoForms brilliant administrator Max, I got the task solved.
So for all of you who have a need for the same task but were afraid to ask, read on πŸ˜€ .
I will try to explain this in a way that people who are not programmers (like myself) will understand.

The objective:
A user filling out a ChronoForm would select a folder name from a drop down list, then in another input field specify a name of a new folder (directory) to be created inside the selected folder. When uploading a file, it would then be uploaded to the newly created directory by the name specified by the user.

Lets say that we have a drop down list of folders we created inside the "uploads" folder: guitar, bass, keys, drums, voice. Lets assume that the ID for this selection is "select_2". Therefore, any choice the user makes when selecting a folder will be represented as "select_2".
Now lets say that the input field where a user will input the name of a new folder has an id of "text_5".

You will need to input the following text inside the Extra Code 1 field within the Form Code tab of your form:

<?php
//First we set up parameters (JPATH_SITE = The path to the installed Joomla! site)
//DS = is used as a directory separator.
//Searchpath will consists of joomla site, "uploads" folder we created manually and dynamic input field 'select_2' from the form.

$searchpath = JPATH_SITE . DS . "uploads". DS . JRequest::getVar('select_2');

//Then we create the subfolder based on the text input field


if ( !JFolder::create($searchpath . DS . JRequest::getVar('text_5')) ) {
   //Throw error message and stop script
}
$MyForm->setFormParam('uploadpath', $searchpath . DS . JRequest::getVar('text_5') .DS);
?>


So, this is what would happen when the user fills out the form and hits the submit button. Lets assume that she selects the "voice" folder from the drop down menu, then inputs "sarah" in the text field. Upon selecting a file for upload and hitting submit button, the system would then create the following : YourSite.com/uploads/voice/sarah/yourfile.zip
Cool, a?

You can of course have just the text input fields or just the selection fields from your forms.

Whenever you want use a field from a form after the form has been submitted, you have to use the code:
JRequest::getVar('field_name')


Of course, you need to replace the 'field_name' with whatever field name you want to use from your form.

And lastly don't forget to include the following code in the submit field under the general tab:
index.php?option=com_chronocontact&chronoformname=your_form_name&task=send&action=extra


and set your form instance to "same instance" under the general tab.
Replace your_form_name with the actual name of your form.

I hope many of you find this useful. If you don't understand something I posted here, please ask and I will clarify it.

Cheers!

GoranGrooves 8)
GreyHead 28 Aug, 2010
Hi gorangrooves,

Well done, a neat solution. Seems a bit complex though. I'm not sure why you are using the 'extra code' box here at all?

The simplest way to do this is to upload the file and then to create the folder and move the file into it in one of the OnSubmit boxes.

It's also possible to do it by putting code into the File Uploads | Full upload Path box in the Form Editor but that's a bit more complicated to code.

I've written both of these approaches up in a doc here.

Bob
gorangrooves 28 Aug, 2010
Hi Bob,
Thanks for your advices. Well, the reason why I may have gone down the longer path is because I am not a programmer πŸ™‚ . I was trying to patch a different solution into that I needed. Gladly, it works! I will also review your docs and see if I can learn something new.

Best Regards
Goran
GreyHead 29 Aug, 2010
Hi Goran,

I'm not sure that you an claim to be 'not a programmer' any more :-)

Getting that code written and working is definitely 'programming' !

Bob
gorangrooves 29 Aug, 2010
Thanks Bob,
I just had an idea and patched different codes that Max provided. Perhaps I am a "patcher programmer!" πŸ˜€

Goran
This topic is locked and no more replies can be posted.