如何在magento中更新上传的文件

时间:2015-12-04 06:48:57

标签: php file magento file-upload

这是我完成关于magento grid的研究的最后一项挑战。

我有一个文件上传,我可以上传和删除PDF文件,包括DB和目录中文件的路径。我还成功覆盖了Varien_Data_Form_Element_File,将图像放在上传文件按钮之前。我已经发布了很多关于此的问题,我现在正在完成它。

我现在面临的唯一问题是文件的更新。我能够更新数据库上的路径并在目录上上传更新的文件。但是,旧文件不会被删除。如果新旧文件具有相同的文件名,则服务器将通过在文件名末尾添加下划线和一个(_1)来自动重命名新文件。

问题:

如何在更新期间删除服务器上旧的上传文件?

以下是我的控制器。

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);

                    $date = date("m-d-y");
                    $file_name = $_FILES['file_path']['name'];
                    $str_lc = strtolower($file_name);
                    $get_ext = pathinfo($str_lc, PATHINFO_EXTENSION);
                    $basename = basename($str_lc, '.pdf');
                    $str_rep = preg_replace('/[^a-zA-Z0-9:]/', '_', $basename);
                    $full_name = $str_rep.'_'.$date.'.'.$get_ext;

                    /** 
                    *-------------------------------------------
                    * This line of code is intended to delete the old uploaded file 
                    *-------------------------------------------
                    **/
                        if( $this->getRequest()->getParam('id') > 0 ) {

                            //Specify which model to call
                            $pmadminModel = Mage::getModel('pmadmin/pmadmin');

                            //Retrieve current ID of the file
                            $file_id = $this->getRequest()->getParam('id');

                            //Retrieve speicific file_path from the databse
                            $file_collection = $pmadminModel->getCollection()
                                                            ->addFieldToSelect('file_path')
                                                            ->addFieldToFilter('pmadmin_id', $file_id);

                            foreach ($file_collection as $key) {
                                $key;
                            }
                            $file_name_db = $key->getData('file_path');
                            $file_name_form = $_FILES['file_path']['name'];

                            if ($file_name_db != $file_name_form) {
                                if (file_exists($file_name_db)) {
                                    unlink($file_name_form);
                                }
                            }
                        }
                    /**
                    *-------------------------------------end
                    **/

                    $destFile = $path.$full_name;
                    $filename = $uploader->getNewFileName($destFile);
                    $uploader->save($path, $filename);

                    $post_data['file_path']=$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("*/*/");
}

1 个答案:

答案 0 :(得分:0)

使用此代码

 if( $this->getRequest()->getParam('id') > 0  and $_FILES['file_path']['name']) {
                        $filepath = Mage::getBaseDir('media') . DS . 'rts' . DS .'pmadmin'.DS;
                                //Specify which model to call
                                $pmadminModel = Mage::getModel('pmadmin/pmadmin');

                                //Retrieve current ID of the file
                                $file_id = $this->getRequest()->getParam('id');

                                //Retrieve speicific file_path from the databse
                                $file_collection = $pmadminModel->getCollection()
                                                                ->addFieldToSelect('file_path')
                                                                ->addFieldToFilter('pmadmin_id', $file_id);

                                foreach ($file_collection as $key) {
                                    $key;
                                }
                                $file_name_db = $key->getData('file_path');
                                $file_name_form = $_FILES['file_path']['name'];

                                if ($file_name_db != '') {

                                        unlink($filepath.$file_name_form);

                                }}