[SOLVED] How to make dynamic link to uploaded file?

mig 14 Jun, 2012
I want to make a link to the file just uploaded in a form in Events --> OnSubmit --> Email --> Template.

Using the debug feature, I can see an array like this:

[_PLUGINS_] => Array
(
  [upload_files] => Array
  (
    [file_upload] => Array
    (
      [name] => 20120613083148_readme.txt
      [path] => /home/xxx/htdocs/components/com_chronoforms/uploads/yyy/20120613083148_readme.txt
      [size] => 37
      [link] => http://www.xxx.com/components/com_chron ... readme.txt
    )
  )
)


So I want to use something like {file_upload[link]} as a dynamic field in my email template. But chronoforms interprets this as a literal string, not the result of an array.

What is the correct syntax to achieve the value from the array, or the best way to get this?

Thank you,

Marcelo
GreyHead 15 Jun, 2012
Hi Marcelo,

As you see there are a couple more layers to the nested arrays than you have allowed for.

I'd drag in a Custom Code action after the Upload Files action and before the Email action and add this to it
<?php
$link = $form->data[_PLUGINS_][upload_files][file_upload][link];
$link = "<a href='{$link}'>Click to download</a>";
$form->data['link'] = $link;
?>
Then use {link} in your Email template.

Bob

PS You can add the PHP in the template if you prefer but I find it helps to keep the code separate.
mig 15 Jun, 2012
Hi Greyhead,

Your code worked just perfectly. And you also showed me how to deal with the full path of an array, and where to put the custom code, not messing up the email template.

Thank you very much!

Marcelo
goodcopy 29 Nov, 2012
This is very helpful.

My form is currently using two upload fields. How can I create a links to each individual uploads.
Any help will be greatly appreciated.

Thanks,
-Todd
GreyHead 29 Nov, 2012
Hi Todd,

I think that you can just repeat the code there twice for link1 & link2. You'll need to change the file_upload part to match the names of your File inputs.

Bob
goodcopy 29 Nov, 2012
Bob,

Thanks very much. Your suggestion worked.

Todd
goodcopy 29 Nov, 2012
Now, is there a way to make it so the links do not show in the email if no files are submitted?

-Todd
GreyHead 30 Nov, 2012
Hi Todd,

Please try:
<?php
$form->data['link'] = '';
if ( isset($form->data[_PLUGINS_][upload_files][file_upload][link) && $form->data[_PLUGINS_][upload_files][file_upload][link != '' ) {
  $link = $form->data[_PLUGINS_][upload_files][file_upload][link];
  $link = "<a href='{$link}'>Click to download</a>";
  $form->data['link'] = $link;
}
?>

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