I am uploading images in my module using the folowing code at:


$fieldset->addField('image', 'image', array(
  label' => Mage::helper('visualcolor')->__('Image'),
  'required' => false,
  'name' => 'image',

then I save it fine at the controller into my specific folder at media and works fine.

$path = Mage::getBaseDir('media') . DS . "visualcolors" . DS;
$uploader->save($path, $_FILES['image']['name']);

The problem it is how do i get the correct path in the form.php instead the default "media" folder?


You need a custom form element for the image field.
So create this file: app/code/local/[Namespace]/[Module]/Block/Adminhtml/Visualcolor/Helper/Image.php

class [Namespace]_[Module]_Block_Adminhtml_Visualcolor_Helper_Image
    extends Varien_Data_Form_Element_Image {
    protected function _getUrl(){
        $url = false;
        if ($this->getValue()) {
            $url = Mage::getBaseUrl('media').'visualcolors/'.$this->getValue();
        return $url;

now in your Block\Adminhtml\Visualcolor\Edit\Tab\Form.php file tell magento to use your block as the renderer for image fields.

Add this line of code right below $fieldset = $form->addFieldset(....);

$fieldset->addType('image', '[Namespace]_[Module]_Block_Adminhtml_Visualcolor_Helper_Image');
| improve this answer | |
  • Thanks Marius works perfect I had it for the grid but not for the form... – open-ecommerce.org Aug 27 '14 at 14:03

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.