Link to uploaded file in email body

sosume 22 Jun, 2010
Trying to get the file name to be linkable in the body of the email sent via the form submission. Want it to link to the file directly.

I figure "File Upload Settings" is the place to put it if I knew what to code to put. Thought maybe someone could lend a hand if it's easy enough.

Under File Upload Setting, the path is: /home/domain/public_html/uploads/files/ (link would be http://www.domain.com/uploads/files)
The default File Name Format: $filename = date('YmdHis').'_'.$chronofile['name']; (This is where I'm playing around with code but confuses me because I'm not editing a php form processor file the same way.

Has anyone ever accomplished this and would share?

Thanks.
GreyHead 23 Jun, 2010
Hi sosume,

The place to do this is in the Email Template or the OnSubmit Before box. The File Name Format just changes the file name. You need to access the file path that ChronoForms has stored and convert that to a URL.

Try using code like this in the OnSubmit Before box (taken from the upcoming ChronoForms book):
<?php
// get the form uploads information
$MyUploads =& CFUploads::getInstance($MyForm->formrow->id);
// extract the file path
$file_path = $MyUploads->attachments[file_1];
// remove the 'root folder' from the beginning
$file_url = substr($file_path, strlen(JPATH_SITE)+1);
//correct any mis-directed separators
$file_url = str_replace(DS, '/', $file_url);
// add the domain name to the beginning
$file_url = JURI::base().$file_url;
// save the URL into the form results
JRequest::setVar('file_url', $file_url);
?>

Then add {file_url} into a link in the Email Template.

Bob
sosume 25 Jun, 2010
Bob,

Your instructions worked perfectly. I really appreciate your assistance with this!
net97 28 Jun, 2010
Hi,

I have a form wich was saved to content-table. Now there should be the possibility to upload a file wich is linked in the content-articel. How can i do this??
GreyHead 29 Jun, 2010
Hi net87,

You need to change the value from the text area to include a link to the file. You can do this in the OnSubmit Before box.

Bob
ylwisdom02 30 Jun, 2010
Thank you for this. It worked!

Troie
net97 05 Jul, 2010
Hi Bob,

sorry, but i can´t get it, can you post an example-code please??

thx Ralph
GreyHead 05 Jul, 2010
Hi Ralph,

Wnat exactly can't you get?

Bob
net97 06 Jul, 2010
Hi Bob,

this is what i have in my Formcode:

........
<div class="form_item">
  <div class="form_element cf_textarea">
    <label class="cf_label" style="width: 120px;"><strong>Klienten</strong></label>
    <textarea class="cf_inputbox" rows="10" id="text_14" title="" cols="100" name="text_14">1.
2.</textarea>
    
  </div>
  <div class="cfclear"> </div>
</div>

<div class="form_item">
  <div class="form_element cf_fileupload">
    <label class="cf_label" style="width: 120px;">Uploads</label>
    <input class="cf_fileinput cf_inputbox" title="" size="20" id="file_0" name="file_0" type="file" />
    
  </div>
  <div class="cfclear"> </div>
</div>

<div class="form_item">
  <div class="form_element cf_button">
    <input value="Speichern" name="button_3" type="submit" /><input type="reset" name="reset" value="Zurücksetzen"/>
  </div>
  <div class="cfclear"> </div>
</div>


This is my On-submit-Code:
<p>Das Teamsitzungs-Protokoll wurde erfolgreich gespeichert!</p>

<?php
$_POST['title'] = 'Teamsitzung vom '.$_POST['date_0'];
$_POST['introtext'] = nl2br('<strong>Protokoll: </strong>'.$_POST['text_1'].'<br><br><strong>Moderation: </strong>'.$_POST['text_2'].'<br><br><strong>Anwesende: </strong>'.$_POST['check0'].'<br><br><strong>Zielvereinbarung Klienten: </strong><br>'.$_POST['text_3'].'<br><br><strong>Themen: </strong><br>'.$_POST['text_4'].'<br><br><strong>Klienten: </strong><br>'.$_POST['text_9']);
$_POST['fulltext'] = nl2br('<br><br><strong>Themen der Leitung: </strong><br>'.$_POST['text_10'].'<br><br><strong>Kurzinfos: </strong><br>'.$_POST['text_11'].'<br><br><strong>Zielvereinbarung Klienten: </strong><br>'.$_POST['text_12'].'<br><br><strong>Themen: </strong><br>'.$_POST['text_13'].'<br><br><strong>Klienten: </strong><br>'.$_POST['text_14']);
$_POST['catid'] = '14';
$_POST['id'] = '';
$_POST['sectionid'] = '4';
$_POST['state'] = '1';
$_POST['created'] = date("Y-m-d H:i:s");
?>


So what I have to do to get a Link to the uploaded File in content text?? 🙄
thx Ralph
adampgreen01 06 Jul, 2010
The code was kind of overwhelming for me as a newbie, but I finally got it!
thanks
Adam
GreyHead 07 Jul, 2010
Hi net97,

If I read this correctly you are trying to add a link into an article, not into an email body at all?

Use the same code but put the result into the info-text or full-text strings.

Bob
net97 07 Jul, 2010
Hi Bob,

sorry for the misunderstanding. Yes i try to put the link into an content-article.

I have tried it like this, but it didn´t work:
On Submit code - before:
<?php
// get the form uploads information
$MyUploads =& CFUploads::getInstance($MyForm->formrow->id);
// extract the file path
$file_path = $MyUploads->attachments[file_0];
// remove the 'root folder' from the beginning
$file_url = substr($file_path, strlen(JPATH_SITE)+1);
//correct any mis-directed separators
$file_url = str_replace(DS, '/', $file_url);
// add the domain name to the beginning
$file_url = JURI::base().$file_url;
// save the URL into the form results
JRequest::setVar('file_url', $file_url);
?>


On Submit code - after:
<p>Das Teamsitzungs-Protokoll wurde erfolgreich gespeichert!</p>

<?php
$_POST['title'] = 'Teamsitzung vom '.$_POST['date_0'];
$_POST['introtext'] = nl2br('<strong>Protokoll: </strong>'.$_POST['text_1'].'<br><br><strong>Moderation: </strong>'.$_POST['text_2'].'<br><br><strong>Anwesende: </strong>'.$_POST['check0'].'<br><br><strong>Zielvereinbarung Klienten: </strong><br>'.$_POST['text_3'].'<br><br><strong>Themen: </strong><br>'.$_POST['text_4'].'<br><br><strong>Klienten: </strong><br>'.$_POST['text_9']);
$_POST['fulltext'] = nl2br('<br><br><strong>Themen der Leitung: </strong><br>'.$_POST['text_10'].'<br><br><strong>Kurzinfos: </strong><br>'.$_POST['text_11'].'<br><br><strong>Zielvereinbarung Klienten: </strong><br>'.$_POST['text_12'].'<br><br><strong>Themen: </strong><br>'.$_POST['text_13'].'<br><br><strong>Klienten: </strong><br>'.$_POST['text_14'].'<br><br><strong>Datei: </strong><br>'.$_POST['file_url']);
$_POST['catid'] = '14';
$_POST['id'] = '';
$_POST['sectionid'] = '4';
$_POST['state'] = '1';
$_POST['created'] = date("Y-m-d H:i:s");
?>


There appears nothing in the article-text!
GreyHead 07 Jul, 2010
Hi net97,

Put the code at the beginning of your OnSubmit After code - I suspect that the OnSubmit Before box isn't being executed.

Bob
net97 07 Jul, 2010
Hi Bob,

thanks, now i become the right URL but no link to click. Is this possible to do also?

What i have to change?

best regards
GreyHead 07 Jul, 2010
Hi net97,

Just wrap a link around the url
<a href='$file_url' >Click here to download</a>


Bob
net97 09 Jul, 2010
sorry but that did not work! There must be a speciality in syntax...
GreyHead 09 Jul, 2010
Hi net97,

Nothing special, it's standard PHP& HTML. I guess that you'll need to debug your code.

Bob
sosume 17 Aug, 2010
Hello Bob,

One question back to the beginning of this topic, related to making the file name as linkable in the email after submit. What would cause the email link to be broken at the end if the file name happens to be long? It's putting the URL so http://www.site.com/folder1/folder2/folder3/time&date_long file name.pdf. But the last part is missing, say about halfway through the files name. It's there just not part of the hyperlink. Could that be a limitation and I should try shortening the path or file name?

It works for the most part and thanks again.
sosume 17 Aug, 2010
Or, maybe it's the space in the file name??
GreyHead 18 Aug, 2010
Hi sosume,

I'm pretty sure that a space in the file-name would break the url. You could replace the spaces with +; or urlencode the filename (that will replace the spaces with %20 I think - either should work.

But better to avoid spaces in filenames if possible.

Bob
mmaack 23 Sep, 2010
This solution has worked like a champ for me!

Taking it a step further, is there a way to make the {file_url} conditional in the email template if the end user skips the upload field, assuming that the file upload is an option and not a requirement?

As it stands now, when the field is skipped, my email has a link to my website root. When a file is uploaded, then the path is tagged onto the end of that string.

So if a file is uploaded then show the link, if not - hide it? Is this do-able?

Thanks again!
GreyHead 24 Sep, 2010
Hi mmaack ,

You can probably do this in the OnSubmit Before box. You need to check for a value for the file input and set a default '' if there isn't one. Try this
<?php
$file_url = JRequest::getString('file_url', '', 'post');
JRequest::setVar('file_url', $file_url);
?>

I think this should work but file inputs are sometimes a little different from text inputs.

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