Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在magento中更新上载的文件_Php_File_Magento_File Upload - Fatal编程技术网

Php 如何在magento中更新上载的文件

Php 如何在magento中更新上载的文件,php,file,magento,file-upload,Php,File,Magento,File Upload,这是我完成关于magento网格研究的最后一个挑战 我有一个文件上传,我可以上传和删除PDF文件,从数据库的路径和目录中的文件。我还成功地覆盖了Varien_Data_Form_Element_文件,将图像放在upload File按钮之前。我已经发布了很多关于这个的问题,我现在正在完成它的路上 我现在面临的唯一困难是文件的更新。我能够更新数据库上的路径,并将更新后的文件上载到目录中。但是,不会删除旧文件。如果新文件和旧文件具有相同的文件名,服务器将通过在文件名末尾添加下划线和(_1)自动重命名

这是我完成关于magento网格研究的最后一个挑战

我有一个文件上传,我可以上传和删除PDF文件,从数据库的路径和目录中的文件。我还成功地覆盖了Varien_Data_Form_Element_文件,将图像放在upload 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("*/*/");
}
使用此代码

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

                                }}