5

I am attaching an image with tags in admin panel and trying to save tag id and image name in my custom table and also the image in media/tagimage folder.

I have put a button to upload the image in Mage_Adminhtml_Block_Widget_Form.

Here is my block code:

protected function _prepareForm()
{
    $model = Mage::registry('tag_tag');

    $form = new Varien_Data_Form(
        array('id' => 'edit_form',
            'action' => $this->getData('action'),
            'method' => 'post',
            'enctype' => 'multipart/form-data')
    );

    $fieldset = $form->addFieldset('base_fieldset',
        array('legend'=>Mage::helper('tag')->__('General Information')));

    if ($model->getTagId()) {
        $fieldset->addField('tag_id', 'hidden', array(
            'name' => 'tag_id',
        ));
    }

    $fieldset->addField('form_key', 'hidden', array(
        'name'  => 'form_key',
        'value' => Mage::getSingleton('core/session')->getFormKey(),
    ));

    $fieldset->addField('store_id', 'hidden', array(
        'name'  => 'store_id',
        'value' => (int)$this->getRequest()->getParam('store')
    ));

    $fieldset->addField('tag_name', 'text', array(
        'name' => 'tag_name',
        'label' => Mage::helper('tag')->__('Tag Name'),
        'title' => Mage::helper('tag')->__('Tag Name'),
        'required' => true,

    ));

    //custom
    $fieldset->addField('tag_image', 'file', array(
        'name' => 'tag_image',
        'label' => Mage::helper('tag')->__('Tag Image'),
        'title' => Mage::helper('tag')->__('Tag Image'),
    ));
    //end custom

    $fieldset->addField('status', 'select', array(
        'label' => Mage::helper('tag')->__('Status'),
        'title' => Mage::helper('tag')->__('Status'),
        'name' => 'tag_status',
        'required' => true,
        'options' => array(
            Mage_Tag_Model_Tag::STATUS_DISABLED => Mage::helper('tag')->__('Disabled'),
            Mage_Tag_Model_Tag::STATUS_PENDING  => Mage::helper('tag')->__('Pending'),
            Mage_Tag_Model_Tag::STATUS_APPROVED => Mage::helper('tag')->__('Approved'),
        ),
        'after_element_html' => ' ' . Mage::helper('adminhtml')->__('[GLOBAL]'),
    ));

    $fieldset->addField('base_popularity', 'text', array(
        'name' => 'base_popularity',
        'label' => Mage::helper('tag')->__('Base Popularity'),
        'title' => Mage::helper('tag')->__('Base Popularity'),
        'after_element_html' => ' ' . Mage::helper('tag')->__('[STORE VIEW]'),
    ));


    if (!$model->getId() && !Mage::getSingleton('adminhtml/session')->getTagData() ) {
        $model->setStatus(Mage_Tag_Model_Tag::STATUS_APPROVED);
    }

    if ( Mage::getSingleton('adminhtml/session')->getTagData() ) {
        $form->addValues(Mage::getSingleton('adminhtml/session')->getTagData());
        Mage::getSingleton('adminhtml/session')->setTagData(null);
    } else {
        $form->addValues($model->getData());
    }

    $this->setForm($form);
    return parent::_prepareForm();
}

and this is my code in saveAction in tagController for saving the image:

$customPostData = $this->getRequest()->getPost('tag_image');
  //print_r($customPostData); die();

   $name =  $_FILES["tag_image"]["name"];
        $path =  $_FILES["tag_image"]["tmp_name"];
        $media_path = Mage::getBaseDir('media')."/tagimage";
        move_uploaded_file($path,$media_path."/".$name);
  $tagimage_collection = Mage::getModel('sports365/tagimage');
  $tagimage_collection->setTagImage($name)
                      ->setTagId($customPostData['tag_id']);

Now my problem is that even I have $customPostData = $this->getRequest()->getPost('tag_image');
  //print_r($customPostData); die();

   $name =  $_FILES["tag_image"]["name"];
        $path =  $_FILES["tag_image"]["tmp_name"];
        $media_path = Mage::getBaseDir('media')."/tagimage";
        move_uploaded_file($path,$media_path."/".$name);
  $tagimage_collection = Mage::getModel('sports365/tagimage');
  $tagimage_collection->setTagImage($name)
                      ->setTagId($customPostData['tag_id']);

My problem is even I have added enctype => 'multipart/form-data' in form initialising but it still not working and nothing is coming in $_FILES["tag_image"]["name"];

Now if I go to inspect element,edit as html and add enctype => 'multipart/form-data' to the form, my code will save the image both in media folder and database.

Note: Right now I am testing in core files

2
  • Did you check if the actual HTML output contains <form enctype="multipart/form-data">?
    – 7ochem
    Jul 8 '15 at 10:05
  • Has there been a solution for this? I have the same issue. It works in one of my Admin form creators but not the other. enctype => 'multipart/form-data' doesn't even display in the form when I inspect element on it. Apr 4 '17 at 0:38
2

Try to use the image upload field when wanting to save images:

$fieldset->addField(
            "image", "image", array(
                "label"    => Mage::helper("tag")->__("Image"),
                "class"    => "required-entry",
                "required" => true,
                "name"     => "image",
            )
        );

If you look at /lib/Varien/Data/Form/Element/File.php it sets the EXT type as file but if you use the image element '/lib/Varien/Data/Form/Element/Image.php` it doesn't set the file extension. It also adds some markup to preview and delete the image from the admin form.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.