如何在adminhtml控制器文件中的自定义扩展中上传图像

时间:2015-01-22 10:55:23

标签: image magento

 public function saveAction()
    {
        if ($this->getRequest()->getPost()) {

            try {
                $postData = $this->getRequest()->getPost();

                $bookingModel = Mage::getModel('booking/booking');

                $bookingModel->setId($this->getRequest()->getParam('id'))
                    ->setTitle($postData['title'])
                    ->setContent($postData['content'])
                    ->setStatus($postData['status'])
                    ->setImage($postData['image'])
                    ->save();

                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Item was successfully saved'));
                Mage::getSingleton('adminhtml/session')->setbookingData(false);
           $this->_redirect('*/*/');
                return;
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                Mage::getSingleton('adminhtml/session')->setbookingData($this->getRequest()->getPost());
                $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
                return;
            }
        }
        $this->_redirect('*/*/');
    }

//让我知道如何在我的扩展中插入图片如果有任何建议请给我回答这是有效但只是插入图片的名称..

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码直接添加上传:


    protected function _uploadImageFile($name)
    {
        $uploader = new Varien_File_Uploader($name);
        $uploader->setAllowedExtensions(array('jpg', 'jpeg', 'gif', 'png'));
        $uploader->setAllowRenameFiles(true);

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

        return $uploader->getUploadedFileName();
    }

用法:


    $postData['image'] = $this->_uploadImageFile('image');