Forums

How do I retrieve the full url to uploaded images

carramone 17 Jul, 2018
Hi
I have a form that lets visitors upload images in four different fields that are then saved on my server. I save the images and videos on my webserver and I would like to get easy access to these images by adding a direct url to the image and save that in both the database and the email that is sent. I've read through the documentations and this forum and I see a lot of information that gives me almost what I want, but when I try it out I can't really get it to work.

So, what I would like to do is the following.
1. Change the name of the uploaded files so that they have the name of the people uploading the image followed by timestamp and then original filename. Like this: uploadersname_timestamp_filename.[jpg/png/mp4 etc]. The id of my name field is [id=name1] and that is also the name of that field.
2. Upload files to my server in a custom folder (I have already managed this part)
3. set url links to the actual files in the email sent,
4. set url links to the actual files in the database.

Is this at all possible? I'm at chronoforms 6, joomla 3.8.8 and php 7.1x

Thanks for your time.
GreyHead 18 Jul, 2018
Hi carramone,

You can set the file name in the Upload Files action - please see page 18 of the CFv6 manual.

After the Upload Files action the file name, path and URL are included in the form data and you can include them in Save Data and Email actions.

Bob
carramone 18 Jul, 2018
Thank you GreyHead,
I've tried to get it to work but there is something I'm overlooking. I tried to use the "file name provider" and this: {data:name}_{date:Ymd}_{var:upload2.file.name}
My problem now is that the field "name" can contain spaces. is it possible to remove these spaces in any easy way so that I don't get broken urls?

Also, the file type is removed from the name. How do I retain the file ending?

And about setting the url to to uploaded image and saving that in the database as well as in the sent email, well, I'm still totally at a loss here, I don't know where to start😟

Best regards,
healyhatman 18 Jul, 2018
Some definitions to make things clearer:

upload: The name of the UPLOAD action (in the setup tab)
filefield: The name of the file FIELD (in the design tab)

DON'T copy/paste anything from the forums, you have to retype it manually because the forums add hidden characters.

To strip spaces:
Create a PHP action BEFORE the upload action. Let's say it's called strippedfilename
return preg_replace('/\s+/', '', $this->data("filefield.filename", ""))

To name your file with the extension and everything, have your file name provider set to
{data:name}_{date:Ymd}_{var:strippedfilename}


To get the uploaded file name to save into your database
{var:upload.filefield.filename}




But the number one best thing to do: USE A DEBUG ACTION! All of the information you were asking about is shown there. The file field data and upload action will show, under "var", all the things you can get using the shortcodes.
carramone 19 Jul, 2018
Thank you Healyhatman, I tried to use the code you rpvided and was able to modify it to work, almost anyway.

My fields for uploading have the names: file1 and file2 and my filed for the name I wanted to strip has the name "name"

So, I setup two php scripts. One to strip the name and one to strip the filename.

strippedname
return preg_replace('/\s+/', '', $this->data("name", "")); 

strippedfilename
return preg_replace('/\s+/', '', $this->data("file1", ""));

and in the upload action, name "upload2" i have set the following file name provider
 {var:strippedname}_{date:Y}-{date:m}-{date:d}-{date:h}-{date:m}_{var:strippedfilename}

This works, the data in "name" is stripped from "My Name" to "MyName" and the filename in "file1" is also stripped from "file name.jpg" to "filename.jpg".

But how do I make this work for multiple upload fields? If I just enter a second line of code:
return preg_replace('/\s+/', '', $this->data("file2", ""));

both uploaded files will get the same name. Is it the var:strippedfilename that needs an additional variable, or is it the code in the function strippedfilename that I need to update?

About the database save, where do I add the:
{var:upload.filefield.filename}
Is it in the save data event that I added to the "setup" or is it somewhere else. And if it is to be added to the save data, where? There are a few: data provider, data override on insert, update conditions, special fields...

Best regards,
healyhatman 19 Jul, 2018
Gets added to data override on insert. Read the instructions.

You might need a loop event, with the file field as the data provider, to strip and upload each file in turn. The way you have it now they will of course be given the same name because the PHP action only gets run once, and each time you use {var:phpaction} it doesn't re-run the code it just retrieves the already returned value it has stored.

Dont bother with the name, use the filename like I said.
This topic is locked and no more replies can be posted.