【问题标题】:delete image from folder when new image is uploaded for php yiiframework为php yiiframework上传新图像时从文件夹中删除图像
【发布时间】:2013-06-24 10:10:23
【问题描述】:

我对 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               
        ));
    }

【问题讨论】:

    标签: php yii yii-components


    【解决方案1】:

    例如使用unlink()

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多