2013-06-24 136 views
2

我對PHP yiiframework很新穎。我有用戶模型,它將用戶基本信息包括配置文件映像在內,但是當我更新我的配置文件的新映像時,我必須刪除映像文件。 我使用以下方法上傳了文件。當新圖像上傳爲php時從文件夾中刪除圖像yiiframework

public function actionUpdate() 
    { 
     $model=$this->loadModel($id);  
     $random = substr(number_format(time() * rand(),0,'',''),0,10);   
     if(isset($_POST['Users'])) 
     {       
      $model->attributes=$_POST['Users'];    
      $uploadedFile=CUploadedFile::getInstance($model,'image_path'); 
      $fileName = $milliseconds. '-' .$random; 
      $model->image_path = $fileName; 

      if($model->save())    
       $uploadedFile->saveAs(Yii::app()->basePath.'/../images/uploaded/'.$fileName); 
       $this->redirect(array('view','id'=>$model->id)); 
     }  
     $data = PanXCore::getDataForCreateUser(); 
     $userRoles = new UserRoles(); 
     $this->render('create',array(
      'model'=>$model, 
       'roles' => $data['roles'], 
       'userRoles' => $userRoles    
     )); 
    } 

回答

7
使用

unlink()例如

unlink(Yii::app()->basePath.'/../images/uploaded/'. $oldfile); 
相關問題