title: Magento - Upload file in contact form
body: <p>I have to create a new field in contact form to upload file.</p>
  
  <p>I followed tuts and this is what i did :</p>
  
  <p>in form.phtml :</p>
  
  <pre><code>&lt;form action="&lt;?php echo $this-&gt;getFormAction(); ?&gt;" id="contactForm" method="post" enctype="multipart/form-data"&gt;
  </code></pre>
  
  <p>Then i added the field in the contact form :</p>
  
  <pre><code>&lt;li&gt;
  &lt;label for="attachment"&gt;&lt;?php echo Mage::helper('contacts')-&gt;__('Attachment') ?&gt;&lt;/label&gt;
  &lt;div class="input-box"&gt;
      &lt;input name="MAX_FILE_SIZE" type="hidden" value="2000000" /&gt;
      &lt;input name="attachment" id="attachment" class="input-text" type="file" /&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p></li></p>
  
  <p>I modified the IndexController.php in : /www/app/code/core/Mage/Contacts/controllers as : </p>
  
  <pre><code>&lt;?php
  /**
   * Contacts index controller
   *
   * @category   Mage
   * @package    Mage_Contacts
   * @author      Magento Core Team &lt;core@magentocommerce.com&gt;
   */
  class Mage_Contacts_IndexController extends Mage_Core_Controller_Front_Action
  {
  
      const XML_PATH_EMAIL_RECIPIENT  = 'contacts/email/recipient_email';
      const XML_PATH_EMAIL_SENDER     = 'contacts/email/sender_email_identity';
      const XML_PATH_EMAIL_TEMPLATE   = 'contacts/email/email_template';
      const XML_PATH_ENABLED          = 'contacts/contacts/enabled';
  
      public function preDispatch()
      {
          parent::preDispatch();
  
          if( !Mage::getStoreConfigFlag(self::XML_PATH_ENABLED) ) {
              $this-&gt;norouteAction();
          }
      }
  
      public function indexAction()
      {
          $this-&gt;loadLayout();
          $this-&gt;getLayout()-&gt;getBlock('contactForm')
              -&gt;setFormAction( Mage::getUrl('*/*/post') );
  
          $this-&gt;_initLayoutMessages('customer/session');
          $this-&gt;_initLayoutMessages('catalog/session');
          $this-&gt;renderLayout();
      }
  
      public function postAction()
      {
          $post = $this-&gt;getRequest()-&gt;getPost();
          if ( $post ) {
              $translate = Mage::getSingleton('core/translate');
              /* @var $translate Mage_Core_Model_Translate */
              $translate-&gt;setTranslateInline(false);
              try {
                  $postObject = new Varien_Object();
                  $postObject-&gt;setData($post);
  
                  $error = false;
  
                  if (!Zend_Validate::is(trim($post['name']) , 'NotEmpty')) {
                      $error = true;
                  }
  
                  if (!Zend_Validate::is(trim($post['comment']) , 'NotEmpty')) {
                      $error = true;
                  }
  
                  if (!Zend_Validate::is(trim($post['email']), 'EmailAddress')) {
                      $error = true;
                  }
  
                  if (Zend_Validate::is(trim($post['hideit']), 'NotEmpty')) {
                      $error = true;
                  }
                  /**************************************************************/
                  $fileName = '';
                  if (isset($_FILES['attachment']['name']) &amp;&amp; $_FILES['attachment']['name'] != '') {
                      try {
                          $fileName       = $_FILES['attachment']['name'];
                          $fileExt        = strtolower(substr(strrchr($fileName, ".") ,1));
                          $fileNamewoe    = rtrim($fileName, $fileExt);
                          $fileName       = preg_replace('/\s+', '', $fileNamewoe) . time() . '.' . $fileExt;
  
                          $uploader       = new Varien_File_Uploader('attachment');
                          $uploader-&gt;setAllowedExtensions(array('doc', 'docx','pdf'));
                          $uploader-&gt;setAllowRenameFiles(false);
                          $uploader-&gt;setFilesDispersion(false);
                          $path = Mage::getBaseDir('media') . DS . 'contacts';
                          if(!is_dir($path)){
                              mkdir($path, 0777, true);
                          }
                          $uploader-&gt;save($path . DS, $fileName );
  
                      } catch (Exception $e) {
                          $error = true;
                      }
                  }
                  /**************************************************************//**************************************************************/
                  $fileName = '';
                  if (isset($_FILES['attachment']['name']) &amp;&amp; $_FILES['attachment']['name'] != '') {
                      try {
                          $fileName       = $_FILES['attachment']['name'];
                          $fileExt        = strtolower(substr(strrchr($fileName, ".") ,1));
                          $fileNamewoe    = rtrim($fileName, $fileExt);
                          $fileName       = preg_replace('/\s+', '', $fileNamewoe) . time() . '.' . $fileExt;
  
                          $uploader       = new Varien_File_Uploader('attachment');
                          $uploader-&gt;setAllowedExtensions(array('doc', 'docx','pdf', 'jpg', 'jpeg', 'png', 'bmp', 'gif'));
                          $uploader-&gt;setAllowRenameFiles(false);
                          $uploader-&gt;setFilesDispersion(false);
                          $path = Mage::getBaseDir('media') . DS . 'contacts';
                          if(!is_dir($path)){
                              mkdir($path, 0777, true);
                          }
                          $uploader-&gt;save($path . DS, $fileName );
  
                      } catch (Exception $e) {
                          $error = true;
                      }
                  }
                  /**************************************************************/
  
                  if ($error) {
                      throw new Exception();
                  }
  
                  $mailTemplate = Mage::getModel('core/email_template');
  
                  /* @var $mailTemplate Mage_Core_Model_Email_Template */
                  /**************************************************************/
                  //sending file as attachment
                  $attachmentFilePath = Mage::getBaseDir('media'). DS . 'contacts' . DS . $fileName;
                  if(file_exists($attachmentFilePath)){
                      $fileContents = file_get_contents($attachmentFilePath);
                      $attachment   = $mailTemplate-&gt;getMail()-&gt;createAttachment($fileContents);
                      $attachment-&gt;filename = $fileName;
                  }
                  /**************************************************************/
                  $mailTemplate-&gt;setDesignConfig(array('area' =&gt; 'frontend'))
                      -&gt;setReplyTo($post['email'])
                      -&gt;sendTransactional(
                          Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE),
                          Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER),
                          Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT),
                          null,
                          array('data' =&gt; $postObject)
                      );
  
                  if (!$mailTemplate-&gt;getSentSuccess()) {
                      throw new Exception();
                  }
  
                  $translate-&gt;setTranslateInline(true);
  
                  Mage::getSingleton('customer/session')-&gt;addSuccess(Mage::helper('contacts')-&gt;__('Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.'));
                  $this-&gt;_redirect('*/*/');
  
                  return;
              } catch (Exception $e) {
                  $translate-&gt;setTranslateInline(true);
  
                  Mage::getSingleton('customer/session')-&gt;addError(Mage::helper('contacts')-&gt;__('Unable to submit your request. Please, try again later'));
                  $this-&gt;_redirect('*/*/');
                  return;
              }
  
          } else {
              $this-&gt;_redirect('*/*/');
          }
      }
  
  }
  </code></pre>
  
  <p>I created a <code>contacts</code> directory in <code>medias</code> with 777 rights</p>
  
  <p>The contact form is correctly shown in my page, i can choose a file, but it isnt uploaded. In <code>medias/contacts/</code> no file, and in my email i have a attached file called <code>noname</code>
  Impossible to use it. Im sure i did something wrong but i cant find what exactly is wrong.</p>
  
  <p>Im not a pro Magento admin, so if you can explain me in details that will be cool :)</p>
  
  <p>Thanks all </p>