I have an issue where the images I'm trying to save in a backend form aren't being populated in the database properly..

My Form.php


class FactoryX_Blog_Block_Manage_Blog_Edit_Form extends 
    protected function _prepareForm()
        $form = new Varien_Data_Form(
                 'id'     => 'edit_form',
                 'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))),
                 'method' => 'post',
                 'enctype'  => 'multipart/form-data'

        return parent::_prepareForm();

The field in /Edit/Form.php

$fieldset->addField('main_image', 'image', array(
      'label'     => Mage::helper('blog')->__('Upload a Main Image'),
      'required'  => true,
      'name'      => 'main_image',

Controller in save action:

if(isset($_FILES['main_image']['name']) and 
        try {
            $uploader = new Varien_File_Uploader('main_image');

            $path = Mage::getBaseDir('media') . DS . "/blog" ;

            $uploader->save($path, $_FILES['main_image']['name']);

            $data['main_image'] = $_FILES['main_image']['name'];
      }catch(Exception $e) {

    else {      
        if(isset($data['main_image']['delete']) && $data['main_image']['delete'] == 1)
            $data['image_main'] = '';

The images are saving into the media folder how I'd like them to, but the main_image field in my database is showing up as "Array". The main image field is type Varchar(255).

enter image description here

I would like the path of the image to be saved to the database instead.

1 Answer 1


Actually it's just PHP nature. The $_FILE['field_name']['name'] is an array, not string in most cases. You can modify Controller to the following:

foreach($_FILES['main_image']['name'] as $filename)   
    $uploader->save($path, $filename);


  • I found out my problem was that the form was being saved again after I'd already saved it with the correct data, over writing my changes. But you couldn't have gotten that from the code I provided, so I'm marking you the correct answer.
    – Jack Perry
    Commented Jan 10, 2018 at 23:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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