Sidebar

 

Virtuemart Extensions by RuposTel.com

  • Full Screen
  • Wide Screen
  • Narrow Screen
  • Increase font size
  • Default font size
  • Decrease font size

How to add Uploads to your Joomla Contact Form

Dear frieds, we recently came around a request to add uploads to Joomla 3.1 forms and we would like to share our code with you here. 

 

Joomla 3.1 and 2.5 

Create the upload fields within your template's file  \templates\YOUR JOOMLA THEME\html\com_contact\contact\default_form.php

that should look like this

<?php
/**
* @package Joomla.Site
* @subpackage com_contact
*
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

defined('_JEXEC') or die;

JHtml::_('behavior.keepalive');
JHtml::_('behavior.formvalidation');

if (isset($this->error)) : ?>
<div class="contact-error">
<?php echo $this->error; ?>
</div>
<?php endif; ?>

<div class="contact-form">
<form id="contact-form" action="<?php echo JRoute::_('index.php'); ?>" method="post" class="form-validate form-horizontal" enctype="multipart/form-data">
<fieldset>
<legend><?php echo JText::_('COM_CONTACT_FORM_LABEL'); ?></legend>
<div class="control-group">
<div class="control-label"><?php echo $this->form->getLabel('contact_name'); ?></div>
<div class="controls"><?php echo $this->form->getInput('contact_name'); ?></div>
</div>
<div class="control-group">
<div class="control-label"><?php echo $this->form->getLabel('contact_email'); ?></div>
<div class="controls"><?php echo $this->form->getInput('contact_email'); ?></div>
</div>
<div class="control-group">
<div class="control-label"><?php echo $this->form->getLabel('contact_subject'); ?></div>
<div class="controls"><?php echo $this->form->getInput('contact_subject'); ?></div>
</div>
<div class="control-group">
<div class="control-label"><?php echo $this->form->getLabel('contact_message'); ?></div>
<div class="controls"><?php echo $this->form->getInput('contact_message'); ?></div>
</div>
<div class="control-group">
<div class="control-label"><?php echo $this->form->getLabel('front_side_image'); ?></div>
<div class="controls"><?php echo $this->form->getInput('front_side_image'); ?></div>

</div>
<div class="control-group">

<div class="control-label"><?php echo $this->form->getLabel('back_side_image'); ?></div>
<div class="controls"><?php echo $this->form->getInput('back_side_image'); ?></div>
</div>
<div class="control-group">
<div class="control-label"><?php echo $this->form->getLabel('logo_image'); ?></div>
<div class="controls"><?php echo $this->form->getInput('logo_image'); ?></div>
</div>
<div class="control-group">
<div class="control-label"><?php echo $this->form->getLabel('qr_image'); ?></div>
<div class="controls"><?php echo $this->form->getInput('qr_image'); ?></div>
</div>
<div class="control-group">
<div class="control-label"><?php echo $this->form->getLabel('vcard_file');; ?></div>
<div class="controls"><?php echo $this->form->getInput('vcard_file'); ?></div>
</div>


<?
if ($this->params->get('show_email_copy')) { ?>
<div class="control-group">
<div class="control-label"><?php echo $this->form->getLabel('contact_email_copy'); ?></div>
<div class="controls"><?php echo $this->form->getInput('contact_email_copy'); ?></div>
</div>
<?php } ?>
<?php //Dynamically load any additional fields from plugins. ?>
<?php foreach ($this->form->getFieldsets() as $fieldset) : ?>
<?php if ($fieldset->name != 'contact'):?>
<?php $fields = $this->form->getFieldset($fieldset->name);?>
<?php foreach ($fields as $field) : ?>
<div class="control-group">
<?php if ($field->hidden) : ?>
<div class="controls">
<?php echo $field->input;?>
</div>
<?php else:?>
<div class="control-label">
<?php echo $field->label; ?>
<?php if (!$field->required && $field->type != "Spacer") : ?>
<span class="optional"><?php echo JText::_('COM_CONTACT_OPTIONAL');?></span>
<?php endif; ?>
</div>
<div class="controls"><?php echo $field->input;?></div>
<?php endif;?>
</div>
<?php endforeach;?>
<?php endif ?>
<?php endforeach;?>
<div class="form-actions"><button class="btn btn-primary validate" type="submit"><?php echo JText::_('COM_CONTACT_CONTACT_SEND'); ?></button>
<input type="hidden" name="option" value="com_contact" />
<input type="hidden" name="task" value="contact.submit" />
<input type="hidden" name="return" value="<?php echo $this->return_page;?>" />
<input type="hidden" name="id" value="<?php echo $this->contact->slug; ?>" />
<?php echo JHtml::_('form.token'); ?>
</div>
</fieldset>
</form>
</div>

 

Create a plugin that is triggered from 

\components\com_contact\controllers\contact.php
public function submit()

and with 
$dispatcher->trigger('onValidateContact', array(&$contact, &$data));

OR

$dispatcher->trigger('onSubmitContact', array(&$contact, &$data));

 

Create a plugin in your (or download from below)

\plugins\contact\attachment\attachment.php

that will include this simple code and write the uploaded files into your upload folder ABOVE your joomla root so it's not accessible with a browser

<?php
/**
* @package attachement for contact form
* @copyright Copyright (c)2010-2013 RuposTel.com
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later
*/

defined('_JEXEC') or die();

JLoader::import('joomla.plugin.plugin');

class plgContactAttachment extends JPlugin
{
public function onSubmitContact(&$contact, &$data)
{

jimport( 'joomla.filesystem.file' );
$email = str_replace('@','__at__', $data['contact_email']);
$dir = DIRECTORY_SEPARATOR.'upload'.DIRECTORY_SEPARATOR.JFile::makeSafe($email).DIRECTORY_SEPARATOR;
$data['images'] = array();

foreach ($_FILES['jform']['tmp_name'] as $key=>$file)
{
if (empty($_FILES['jform']['tmp_name'][$key])) continue;
$key = JFile::makeSafe($key);
JFolder::create($dir);
$ext = JFile::getExt($_FILES['jform']['name'][$key]);
$x = JFile::upload($_FILES['jform']['tmp_name'][$key], $dir.$key.'.'.$ext);
$data['images'][] = $dir.$key.'.'.$ext;
}

}
}

 

This simple function will upload all the files that are rendered with the form. You can directly download the installable joomla contact attchement plugin for Joomla 2.5 and Joomla 3.5 from here.

 

If you wish to send the files by email to the administrator, you will need to modify Joomla's core file in 

\components\com_contact\controllers\contact.php

 

and search for code: 

$sent = $mail->Send();

 

and add this line above: 

$mail->addAttachment($data['images']);
$sent = $mail->Send();

 

Without the core hack the contact form upload plugin will save the files per the names of the fields to your /upload/email__at__domain.com/key_of_the_input.extension

 

Please note that even though this code was suggested to you with the security in mind, there could be much more done to prevent improper content uploads. 

 

 

 

 

Attachments:
FileDescriptionFile size
Download this file (default_form.zip)default_form.zipunzip to your \templates\YOUR THEME\html\com_contact\contact\default_form.php1 Kb
Download this file (plg_contact_attachment.zip)plg_contact_attachment.zipinstallable plugin1 Kb
You are here