Magento文件上传不会删除或更新上传的文件

时间:2015-11-26 01:49:29

标签: php magento file-upload

继续我关于 Magento Admin Grid 的研究。我正在尝试创建文件上传,但我成功完成了它。但是,我遇到了有关更新和删除文件上载表单字段的问题。其他输入表单字段填充了数据,可以从记录中更新和删除。

问题:

当尝试更新记录时,文件上载字段未显示上载文件的名称,并且当尝试更新它时,旧的上载文件未被删除,但文件路径已在记录上更新。

当尝试删除记录时,上传的文件未被删除但在记录中被删除。

我也有关于网格的小问题。即使在网格上声明了网格,网格也不会显示记录的ID和其他整数或数字。

问题:

我的更新表单字段和网格中缺少什么?

以下是我的表单字段示例。

    $fieldset->addField('title', 'text', array(
        'label' => Mage::helper('pmadmin')->__('Matrix Title'),
        'class' => 'required-entry',
        'required' => true,
        'name' => 'title',
    ));

    $fieldset->addField('file_path', 'file', array(
        'label' => Mage::helper('pmadmin')->__('File'),
        'value'  => '',
        'class' => 'required-entry',
        'required' => true,
        'disabled' => false,
        'readonly' => true,
        'name' => 'file_path',

        ));

    $fieldset->addField('short_description', 'text', array(
        'label' => Mage::helper('pmadmin')->__('Short Description'),
        'class' => 'required-entry',
        'required' => true,
        'name' => 'short_description',
    ));

这是我的控制器

public function editAction()
{
    $pmadminId     = $this->getRequest()->getParam('id');
    $pmadminModel  = Mage::getModel('pmadmin/pmadmin')->load($pmadminId);

    if ($pmadminModel->getId() || $pmadminId == 0) {

        Mage::register('pmadmin_data', $pmadminModel);

        $this->loadLayout();
        $this->_setActiveMenu('pmadmin/items');

        $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Item Manager'), Mage::helper('adminhtml')->__('Item Manager'));
        $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Item News'), Mage::helper('adminhtml')->__('Item News'));

        $this->getLayout()->getBlock('head')->setCanLoadExtJs(true);

        $this->_addContent($this->getLayout()->createBlock('pmadmin/adminhtml_pmadmin_edit'))
             ->_addLeft($this->getLayout()->createBlock('pmadmin/adminhtml_pmadmin_edit_tabs'));

        $this->renderLayout();
    } else {
        Mage::getSingleton('adminhtml/session')->addError(Mage::helper('pmadmin')->__('Item does not exist'));
        $this->_redirect('*/*/');
    }
}

public function newAction()
{
    $this->_forward('edit');
}

public function saveAction() {
    $post_data=$this->getRequest()->getPost();

    if ($post_data) {
        try {                
     //save file to the destination folder   
            if (isset($_FILES)){
                if ($_FILES['file_path']['name']) {

                    $path = Mage::getBaseDir('media') . DS . 'rts' . DS .'pmadmin'.DS;
                    $uploader = new Varien_File_Uploader('file_path');
                    $uploader->setAllowedExtensions(array('PDF','pdf'));
                    $uploader->setAllowRenameFiles(false);
                    $uploader->setFilesDispersion(false);
                    $destFile = $path.$_FILES['file_path']['name'];
                    $filename = $uploader->getNewFileName($destFile);
                    $uploader->save($path, $filename);

                    $post_data['file_path']='rts/pmadmin/'.$filename;
                }
            }
    //save file path to the database
            $model = Mage::getModel("pmadmin/pmadmin")
            ->addData($post_data)
            ->setId($this->getRequest()->getParam("id"))
            ->save();

            Mage::getSingleton("adminhtml/session")->addSuccess(Mage::helper("adminhtml")->__("File was successfully saved"));
            Mage::getSingleton("adminhtml/session")->setPmadminData(false);

            if ($this->getRequest()->getParam("back")) {
                $this->_redirect("*/*/edit", array("id" => $model->getId()));
                return;
            }
            $this->_redirect("*/*/");
            return;
        } 
        catch (Exception $e) {
            Mage::getSingleton("adminhtml/session")->addError($e->getMessage());
            Mage::getSingleton("adminhtml/session")->setPmadminData($this->getRequest()->getPost());
            $this->_redirect("*/*/edit", array("id" => $this->getRequest()->getParam("id")));
        return;
        }

    }
    $this->_redirect("*/*/");
}

public function deleteAction()
{
    if( $this->getRequest()->getParam('id') > 0 ) {
        try {
            $pmadminModel = Mage::getModel('pmadmin/pmadmin');

            $pmadminModel->setId($this->getRequest()->getParam('id'))
                ->delete();

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

注意:

我能够成功更新其他表单字段和记录中的文件路径,但不能上传文件。

1 个答案:

答案 0 :(得分:0)

如果用户没有上传进行编辑,则必须添加其他语句。 $postdata['file_path']['value']这是由magento自动创建的。

if (isset($_FILES)){
                if ($_FILES['file_path']['name']) {

                    $path = Mage::getBaseDir('media') . DS . 'rts' . DS .'pmadmin'.DS;
                    $uploader = new Varien_File_Uploader('file_path');
                    $uploader->setAllowedExtensions(array('PDF','pdf'));
                    $uploader->setAllowRenameFiles(false);
                    $uploader->setFilesDispersion(false);
                    $destFile = $path.$_FILES['file_path']['name'];
                    $filename = $uploader->getNewFileName($destFile);
                    $uploader->save($path, $filename);

                    $post_data['file_path']='rts/pmadmin/'.$filename;
                }
     else {
              $postdata['file_path']=$postdata['file_path']['value'];
            }
     }