Magento - 在后台上传图片

时间:2016-01-18 13:29:18

标签: php magento image-uploading

我试图在Magento的销售规则中上传图片,

我遵循本教程:

https://wiki.magento.com/display/m1wiki/How+to+create+an+image+or+video+uploader+for+the+Magento+Admin+Panel

我的字段必须位于本地SalesRules Magento模块中,因此我对以下文件进行了重载:

Mage_Adminhtml_Block_Promo_Quote_Edit_Form

protected function _prepareForm()
{
    $form = new Varien_Data_Form(array('id' => 'edit_form', 'action' => $this->getData('action'), 'method' => 'post','enctype' => 'multipart/form-data'));
    $form->setUseContainer(true);
    $this->setForm($form);
    return parent::_prepareForm();
}

Mage_Adminhtml_Block_Promo_Quote_Edit_Tab_Main 我在_prepareForm()函数

中添加了我的字段
$fieldset->addField('image_promo', 'image', array(
            'label'     => Mage::helper('salesrule')->__('Image'),
            'required'  => false,
            'name'      => 'image_promo',
    ));

在这个文件中,我改变了我的回报:

return parent::_prepareForm();

return Mage_Adminhtml_Block_Widget_Form::_prepareForm();

最后我重载了这个文件:

Mage_Adminhtml_Promo_QuoteController,我添加到saveAction()函数:

if(isset($_FILES['image_promo']['name']) and (file_exists($_FILES['image_promo']['tmp_name']))) {
                try {
                    $uploader = new Varien_File_Uploader('image_promo');
                    $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png')); // or pdf or anything                

                    $uploader->setAllowRenameFiles(false);

                    // setAllowRenameFiles(true) -> move your file in a folder the magento way
                    // setAllowRenameFiles(true) -> move your file directly in the $path folder
                    $uploader->setFilesDispersion(false);

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

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

                    $data['image_promo'] = $_FILES['image_promo']['name'];
                }catch(Exception $e) {
                    Mage::logException($e);
                }
            }else {      

                if(isset($data['image_promo']['delete']) && $data['image_promo']['delete'] == 1)
                    $data['image_main'] = '';
                else
                    unset($data['image_promo']);
            }  

当我尝试保存而不上传任何文件时,magento正常工作,但是当我有一个图像时,我没有通过我的saveAction,并且我被重定向到仪表板...

我试图通过这种方式在我的Main.php(第二个文件)中使用隐藏的form_key

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

但这并没有改变任何事情!

有什么想法吗?

0 个答案:

没有答案