Magento文件上传don';t删除或更新上传的文件


Magento file upload don't delete or update the file uploaded

这只是我对Magento管理网格研究的延续。我正在尝试创建一个文件上传,并成功完成。但是,我遇到了一个关于文件上传表单字段的更新和删除的问题。其他输入表单字段由数据填充,可以从记录中更新和删除。

问题:

当尝试更新记录时,文件上传字段没有显示上传文件的名称,当尝试更新时,旧的上传文件没有被删除,而是更新了记录上的文件路径。

当尝试删除记录时,上载的文件没有被删除,而是在记录上被删除。

我还有一个关于网格的小问题。网格不会显示记录的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('*/*/');
}

注:

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

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