title: Magento image upload form field
body: <p>I followed <a href="http://www.magentocommerce.com/wiki/5_-_modules_and_development/admin/how_to_create_pdf_upload_in_backend_for_own_module" rel="noreferrer">this link</a> to make image upload work within my Magento module, but the greatest thing all that code does is passing selected file name via POST: <code>$_FILES</code> array is empty whenever i add <code>image</code> or <code>file</code> fields.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>&lt;?php
  class Moo_News_Block_Adminhtml_News_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form
  {
      protected function _prepareForm()
      {
          $form = new Varien_Data_Form();
          $this-&gt;setForm($form);
          $fieldset = $form-&gt;addFieldset('news_form', array('legend' =&gt; Mage::helper('news')-&gt;__('Item information')));
  
          $fieldset-&gt;addField('title', 'text', array(
              'label'     =&gt; Mage::helper('news')-&gt;__('Title'),
              'class'     =&gt; 'required-entry',
              'required'  =&gt; true,
              'name'      =&gt; 'title',
          ));
  
          $fieldset-&gt;addField('published', 'select', array(
              'label'     =&gt; Mage::helper('news')-&gt;__('Published'),
              'name'      =&gt; 'published',
              'required'  =&gt; true,
              'values'    =&gt; array(
                  array(
                      'value'     =&gt; 1,
                      'label'     =&gt; Mage::helper('news')-&gt;__('Yes'),
                  ),
  
                  array(
                      'value'     =&gt; 0,
                      'label'     =&gt; Mage::helper('news')-&gt;__('No'),
                  ),
              ),
          ));
  
          $fieldset-&gt;addField('short_description', 'editor', array(
              'name'      =&gt; 'short_description',
              'label'     =&gt; Mage::helper('news')-&gt;__('Short Description'),
              'title'     =&gt; Mage::helper('news')-&gt;__('Short Description'),
              'style'     =&gt; 'width:98%; height:200px;',
              'wysiwyg'   =&gt; false,
              'required'  =&gt; true,
          ));
  
          $fieldset-&gt;addField('body', 'editor', array(
              'name'      =&gt; 'body',
              'label'     =&gt; Mage::helper('news')-&gt;__('Body'),
              'title'     =&gt; Mage::helper('news')-&gt;__('Body'),
              'style'     =&gt; 'width:98%; height:400px;',
              'wysiwyg'   =&gt; false,
              'required'  =&gt; true,
          ));
  
  
          $fieldset-&gt;addField('image', 'file', array(
              'label'     =&gt; Mage::helper('news')-&gt;__('Image'),
              'required'  =&gt; false,
              'name'      =&gt; 'image',
          ));
  
          if ( Mage::getSingleton('adminhtml/session')-&gt;getNewsData() )
          {
              $form-&gt;setValues(Mage::getSingleton('adminhtml/session')-&gt;getNewsData());
              Mage::getSingleton('adminhtml/session')-&gt;setNewsData(null);
          } elseif ( Mage::registry('news_data') ) {
              $form-&gt;setValues(Mage::registry('news_data')-&gt;getData());
          }
  
          return parent::_prepareForm();
      }
  }
  </code></pre>
  
  <p>and the controller code:</p>
  
  <pre><code>&lt;?php
  
  /**
   *
   *
   */
  
  class Moo_News_Adminhtml_NewsController extends Mage_Adminhtml_Controller_Action
  {
  
      protected function _initAction()
      {
          $this-&gt;loadLayout()
              -&gt;_setActiveMenu('news/items')
              -&gt;_addBreadcrumb(Mage::helper('adminhtml')-&gt;__('Items Manager'), Mage::helper('adminhtml')-&gt;__('Item Manager'));
          return $this;
      }
  
      public function indexAction() {
          $this-&gt;_initAction();
          $this-&gt;_addContent($this-&gt;getLayout()-&gt;createBlock('news/adminhtml_news'));
          $this-&gt;renderLayout();
      }
  
      public function editAction()
      {
          $newsId     = $this-&gt;getRequest()-&gt;getParam('id');
          $newsModel  = Mage::getModel('news/post')-&gt;load($newsId);
  
          if ($newsModel-&gt;getId() || $newsId == 0) {
  
              Mage::register('news_data', $newsModel);
  
              $this-&gt;loadLayout();
              $this-&gt;_setActiveMenu('news/items');
  
              $this-&gt;_addBreadcrumb(Mage::helper('adminhtml')-&gt;__('Item Manager'), Mage::helper('adminhtml')-&gt;__('Item Manager'));
              $this-&gt;_addBreadcrumb(Mage::helper('adminhtml')-&gt;__('Item News'), Mage::helper('adminhtml')-&gt;__('Item News'));
  
              $this-&gt;getLayout()-&gt;getBlock('head')-&gt;setCanLoadExtJs(true);
  
              $this-&gt;_addContent($this-&gt;getLayout()-&gt;createBlock('news/adminhtml_news_edit'))
                   -&gt;_addLeft($this-&gt;getLayout()-&gt;createBlock('news/adminhtml_news_edit_tabs'));
  
              $this-&gt;renderLayout();
          } else {
              Mage::getSingleton('adminhtml/session')-&gt;addError(Mage::helper('news')-&gt;__('Item does not exist'));
              $this-&gt;_redirect('*/*/');
          }
      }
  
      public function newAction()
      {
          $this-&gt;_forward('edit');
      }
  
      public function saveAction()
      {
          if ( $this-&gt;getRequest()-&gt;getPost() ) {
              try {
                  $postData = $this-&gt;getRequest()-&gt;getPost();
                  $newsModel = Mage::getModel('news/post');
  
                  $imgFilename = NULL;
  
                  //if (isset($postData['image']) &amp;&amp; $postData['image'] != '') {
                  //if (isset($_FILES['image']['name']) &amp;&amp; $_FILES['image']['name'] != '') {
                  if (false) {
                      try {
                          $uploader = new Varien_File_Uploader('image');
  
                          $uploader-&gt;setAllowedExtensions(array('jpg','jpeg','gif','png'));
                          $uploader-&gt;setAllowRenameFiles(false);
                          $uploader-&gt;setFilesDispersion(false);
  
                          // Set media as the upload dir
                          $media_path  = Mage::getBaseDir('media');
  
                          $imgFilename = $media_path . $postData['image'];
  
                          while (file_exists($filename)) {
                              $pieces = array();
  
                              $res = preg_match('/^(.+)_(\d+)$/', $filename, $pieces);
  
                              if (!$res) {
                                  $filename .= '_1';
                              } else {
                                  $filename .= '_' . strval(intval($pieces[2]) + 1);
                              }
                          }
  
                          // Upload the image
                          $uploader-&gt;save($media_path, $postData['image']);
                      }
                      catch (Exception $e) {
                          Mage::log($e);
                          $this-&gt;redirectError(502);
                      }
                  } else {
                      if(isset($postData['image']['delete']) &amp;&amp; $postData['image']['delete'] == 1) {
                          $imgFilename = NULL;
                      }
                  }
  
                  if(isset($imgFilename))
                      $newsModel-&gt;setImage($imgFilename);
  
                  $newsModel-&gt;setId($this-&gt;getRequest()-&gt;getParam('id'))
                      -&gt;setTitle($postData['title'])
                      -&gt;setBody($postData['body'])
                      -&gt;setShortDescription($postData['short_description'])
                      -&gt;save();
  
                  Mage::getSingleton('adminhtml/session')-&gt;addSuccess(Mage::helper('adminhtml')-&gt;__('Item was successfully saved'));
                  Mage::getSingleton('adminhtml/session')-&gt;setNewsData(false);
  
                  $this-&gt;_redirect('*/*/');
                  return;
              } catch (Exception $e) {
                  Mage::getSingleton('adminhtml/session')-&gt;addError($e-&gt;getMessage());
                  Mage::getSingleton('adminhtml/session')-&gt;setNewsData($this-&gt;getRequest()-&gt;getPost());
                  $this-&gt;_redirect('*/*/edit', array('id' =&gt; $this-&gt;getRequest()-&gt;getParam('id')));
                  return;
              }
          }
          $this-&gt;_redirect('*/*/');
      }
  
      public function deleteAction()
      {
          if( $this-&gt;getRequest()-&gt;getParam('id') &gt; 0 ) {
              try {
                  $newsModel = Mage::getModel('news/post');
  
                  $newsModel-&gt;setId($this-&gt;getRequest()-&gt;getParam('id'))
                      -&gt;delete();
  
                  Mage::getSingleton('adminhtml/session')-&gt;addSuccess(Mage::helper('adminhtml')-&gt;__('Item was successfully deleted'));
                  $this-&gt;_redirect('*/*/');
              } catch (Exception $e) {
                  Mage::getSingleton('adminhtml/session')-&gt;addError($e-&gt;getMessage());
                  $this-&gt;_redirect('*/*/edit', array('id' =&gt; $this-&gt;getRequest()-&gt;getParam('id')));
              }
          }
          $this-&gt;_redirect('*/*/');
      }
      /**
       * Product grid for AJAX request.
       * Sort and filter result for example.
       */
      public function gridAction()
      {
          $this-&gt;loadLayout();
          $this-&gt;getResponse()-&gt;setBody(
                 $this-&gt;getLayout()-&gt;createBlock('importedit/adminhtml_news_grid')-&gt;toHtml()
          );
      }
  }
  </code></pre>
  
  <p>So, the question is: how should i do this in a right way?</p>