【问题标题】:Magento file upload don't delete or update the file uploadedMagento 文件上传不要删除或更新上传的文件
【发布时间】:2015-11-26 01:49:28
【问题描述】:

只是我对 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('*/*/');
}

注意:

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

【问题讨论】:

    标签: php magento file-upload


    【解决方案1】:

    如果用户没有上传进行编辑,您必须添加 else 语句。 $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'];
                }
         }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-09
      • 1970-01-01
      • 2021-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-29
      相关资源
      最近更新 更多