How to upload multiple images from a form for custom extension?

3.8k Views Asked by At

I am working on magneto 1.7 version.In this I created a extension. Now, I need to upload multiple images from a form which I created. I have a browser button in a form Now I need to upload multiple images from that button in a single time.

Can anyone help me?

Below is my form:

protected function _prepareForm() {

    $form = new Varien_Data_Form();
    $this->setForm($form);
    $fieldset = $form->addFieldset('book_form', array('legend' => Mage::helper('test')->__('Book Content')));

    $fieldset->addField('title', 'text', array(
        'label' => Mage::helper('test')->__('Title'),
        'class' => 'required-entry',
        'required' => true,
        'name' => 'title[]',
    ));
    $categoryArray = Mage::getSingleton('test/category')->getOptionArray();
    $fieldset->addField('category_id', 'select', array(
        'label' => Mage::helper('test')->__('Category'),
        'required' => true,
        'class' => 'required-entry',
        'name' => 'category_id[]',
        'values' => $categoryArray,
    ));

    **$fieldset->addField('image', 'file', array(
        'label' => Mage::helper('test')->__('Image'),
        'name' => 'image[]',
        'multiple' => 'multiple',
        'mulitple' => true,
    ));**

    $fieldset->addField('priority', 'text', array(
        'label' => Mage::helper('lookbook')->__('Order of Display'),
        'name' => 'priority[]',
    ));
    $fieldset->addField('hiddenData', 'hidden', array(
        'class' => Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK) . '_' . Mage::registry('book_data')->getId(),
    ));

    $statusArray = Mage::getSingleton('lookbook/status')->getOptionArray();
    $fieldset->addField('publish', 'select', array(
        'label' => Mage::helper('lookbook')->__('Status'),
        'name' => 'publish[]',
        'values' => $statusArray,
    ));

    if (Mage::getSingleton('adminhtml/session')->getBookData()) {
        $form->setValues(Mage::getSingleton('adminhtml/session')->getBookData());
        Mage::getSingleton('adminhtml/session')->setBookData(null);
    } elseif (Mage::registry('book_data')) {
        $form->setValues(Mage::registry('book_data')->getData());
    }


    return parent::_prepareForm();
}
2

There are 2 best solutions below

0
On

Take a look in Mage_Adminhtml_controllers_Catalog

You'll find references to the media_image attribute

|| $attribute->getFrontend()->getInputType() == 'media_image'

You can follow the examples in adminhtml for grid's that allow multiple image uploading for the front end. On the backend side you either need your own resource models, or a backend source model declared for the attribute which can follow the backend and frontend models of the catalog images... in those classes you'll find examples of how the backend model deals with saving the images.

0
On

form.phtml

<form action="<?php echo Mage::getBaseUrl()."multipleimageupload/index/save"; ?>" method="POST" enctype="multipart/form-data">
    <input type="file" name="filename[]" multiple="multiple"> 
    <input type="submit" name="save">
</form>

**

Action

**

public function saveAction(){

        $count= count($_FILES['filename']['name']);

        for ($i=0; $i < $count; $i++) { 

            if(isset($_FILES['filename']['name'][$i]) and (file_exists($_FILES['filename']['tmp_name'][$i]))){                             
                try{
                        // $_FILES['filename']['name'][$i];
                        $path = Mage::getBaseDir('media') . DS . 'multipleimageupload' . DS;
                        // $uploader = new Varien_File_Uploader('filename');
                        $uploader = new Varien_File_Uploader(
                            array(
                                'name' => $_FILES['filename']['name'][$i],
                                'type' => $_FILES['filename']['type'][$i],
                                'tmp_name' => $_FILES['filename']['tmp_name'][$i],
                                'error' => $_FILES['filename']['error'][$i],
                                'size' => $_FILES['filename']['size'][$i]
                            )
                        );

                        $uploader->setAllowedExtensions(array('jpg','png','gif','jpeg'));
                        $uploader->setAllowRenameFiles(false);
                        $uploader->setFilesDispersion(false);
                        $destFile = $path.$_FILES['filename']['name'][$i];
                        $filename = $uploader->getNewFileName($destFile);
                        $uploader->save($path, $filename);
                        $data['img'] = $_FILES['filename']['name'][$i];
                    }catch(Exception $e) {
                        // echo "<pre>";
                        // print_r($e);

                    }
                }else{
                    if(isset($data['filename']['delete'][$i]) && $postData['filename']['delete'][$i] == 1)
                    $data['filename'] = '';
                    else
                    unset($data['filename'][$i]);
                }
            }
        }