更新记录而不删除yii中的上传文件

时间:2015-06-29 01:27:54

标签: php file file-upload yii

我需要UPDATE记录而不更改或删除文件名(我的意思是文件),或者再次重新插入文件以便我如何做到这一点?

这是我的actionCreate,我怎么写更新?

public function actionCreate() {
    $model = new Page;
    if (isset($_POST['Page'])) {
        $model->attributes = $_POST['Page'];
        $model->filename = CUploadedFile::getInstance($model, 'filename');
        if ($model->save()) {
            if ($model->filename !== null) {
                $dest = Yii::getPathOfAlias('application.uploads');
                $model->filename->saveAs($dest . '/' . $model->filename->name);

                $model->save();
            }
            $this->redirect(array('view', 'id' => $model->id));
        }
    }
    $this->render('create', array(
        'model' => $model,
    ));
}

所以请任何人都能找到解决方案

2 个答案:

答案 0 :(得分:0)

如果您不需要在编辑/更新中更改或删除文件名,则忽略文件名(和上传部分),假设文件已经上传,您不希望更改/删除它。

 public function actionUpdate($id) {
    $model = $this->loadModel($id);
    $file = $model->filename;
    if (isset($_POST['Page'])) {
        $model->attributes = $_POST['Page'];
        $model->filename = $file;
        if ($model->save()) {
            $this->redirect(array('view', 'id' => $model->id));
        }
    }
    $this->render('create', array(
        'model' => $model,
    ));
}

答案 1 :(得分:0)

试试这个,它为我工作 -

upload目录之外创建一个protected文件夹,并提供读/写权限。

actionCreate function -

public function actionCreate() {

   $model = new Page();

    if(isset($_POST['Page']) && !empty($_POST['Page'])){

        $model->attributes = $_POST['Page'];

        $rand = rand(1, 999);
        $myfile = CUploadedFile::getInstance($model, 'filename');
        $fileName = "{$rand}-{$myfile}";  //Generate unique file name

           if(!empty( $fileName)){
               $model->filename   = $fileName;
               $target_dir = realpath( Yii::getPathOfAlias('application') . '/../upload');
               $target_file = $target_dir . '/' . $fileName;

               if(!$myfile->saveAs($target_file)){ 
                  $model->addError('filename', 'File saving error');
               }
            }

        if(!$model->hasErrors() && $model->validate() && $model->save(false)) {
         $this->redirect(array('view', 'id'=>$model->id, 'msg'=>'success'));
       }
    }
}

actionUpdate函数 -

public function actionUpdate() {

  $model = Page::model()->findbyPK($id);
  $model->scenario = 'update';

  if(isset($_POST['Page']) && !empty($_POST['Page'])){
     $model->attributes = $_POST['Page'];

     if(empty($_POST['Page']['filename'])) { $file_name = $model->filename; } //store existing file name into a variable if your uploading a file on update

     $rand = rand(1, 999);
     $myfile = CUploadedFile::getInstance($model, 'filename');
     $fileName = "{$rand}-{$myfile}";  //Generate unique file name

     if($model->validate()) {
       if(!empty($fileName)) {
           $model->filename = $fileName;
           $target_dir  = realpath( Yii::getPathOfAlias( 'application' ) . '/../upload');
           $target_file  = $target_dir . '/' . $fileName;

           if(!$myfile->saveAs($target_file)){ 
               $model->addError('filename', 'File saving error');
            }
        } 
      else {
             $model->filename = $file_name; //allow existing file name 
           }

     if( !$model->hasErrors() && $model->save(false) ) {
            $this->redirect(array('view', 'id'=>$model->id, 'msg'=>'update'));
          }
       }
  }

在模型规则数组中定义此 -

array('filename', 'file', 'types'=>'jpg, jpeg, bmp, gif, png', 'allowEmpty'=>true, 'on'=>'insert, update')