简体   繁体   English

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

[英]How to update uploaded file in magento

This is the final challenge for me in completing my study about magento grid. 这是我完成有关magento网格研究的最后挑战。

I have a file upload were I am able to upload and delete PDF file, both the path from DB and the file in the directory. 我可以上传和删除PDF文件,包括来自DB的路径和目录中的文件。 I also successfully override the Varien_Data_Form_Element_File to put image before the upload file button. 我还成功覆盖了Varien_Data_Form_Element_File,以将图像放在上传文件按钮之前。 I have posted a lot of questions about this and I am now on the way of completing it. 我已经发布了很多与此有关的问题,现在我正在完成它。

The only thorn that I am facing right now is the update of the file. 我现在面对的唯一棘手问题是文件的更新。 I am able to update the path on the database and upload the updated file on the directory. 我能够更新数据库上的路径并将更新的文件上传到目录中。 However, the old file is not deleted. 但是,旧文件不会被删除。 And if the new and old file has the same file name, the new file will be automatically renamed by the server by putting an underscore and one (_1) at the end of the file name. 并且,如果新文件和旧文件具有相同的文件名,则服务器将通过在文件名的末尾加上下划线和一个(_1)来自动重命名新文件。

QUESTION: 题:

How do I delete the old uploaded file on the server during update? 更新期间如何删除服务器上的旧上传文件?

Below is my controller. 以下是我的控制器。

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("*/*/");
}

use this code 使用此代码

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

                                }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM