如何从表单上传多个图像以进行自定义扩展?

时间:2013-12-18 12:59:05

标签: image magento magento-1.7 adminhtml

我正在研究磁电机1.7版本。在此我创建了一个扩展。现在,我需要从我创建的表单上传多个图像。 我在表单中有一个浏览器按钮现在我需要一次从该按钮上传多个图像。

任何人都可以帮助我吗?

以下是我的表格:

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 个答案:

答案 0 :(得分:0)

查看Mage_Adminhtml_controllers_Catalog

您将找到对media_image属性的引用

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

您可以按照adminhtml中的示例操作网格,以允许前端进行多个图像上传。在后端,您需要自己的资源模型,或者为属性声明的后端源模型,该模型可以跟随目录图像的后端和前端模型......在这些类中,您将找到后端模型如何处理的示例保存图像。

答案 1 :(得分:0)

form.phtml

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]);
                }
            }
        }

**

动作

**

<?php
 include_once "dbcon.php"; 
 // Check connection
 if(!$link)
 {
     die('not connected');
 }
 $link=  mysqli_query($link, "SELECT * FROM members WHERE status='1' AND dob BETWEEN CURDATE() AND (CURDATE()+ INTERVAL 30 DAY) AND CURDATE() ");
 if(!$eventlink)
 {
     die('not connected');
 }

 $eventlink=  mysqli_query($eventlink, "SELECT * FROM events WHERE status='1' AND edate BETWEEN CURDATE() AND (CURDATE()+ INTERVAL 30 DAY) AND CURDATE() ");
?>