我是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
));
}
答案 0 :(得分:7)
使用unlink()
例如
unlink(Yii::app()->basePath.'/../images/uploaded/'. $oldfile);