Yii2 从文件夹中删除文件

Yag*_*ala 0 yii2

公共函数actionDelete($id)

{

    $model=$this->findModel($id);         
    unlink($_SERVER["DOCUMENT_ROOT"]."/../../uploads/".$model->image);       
Run Code Online (Sandbox Code Playgroud)

或者

    unlink(dirname(__FILE__).'/../../uploads/'.$model->image);
    $this->findModel($id)->delete();    
    return $this->redirect(['index']);       
}                                       
Run Code Online (Sandbox Code Playgroud)

小智 6

使用 PHP 函数 unlink()

然后转到您的模型控制器

public function actionDelete($id)
{
    $data = Document::findOne($id);
    unlink(Yii::$app->basePath . '/web/' . $data->file_name);
    $this->findModel($id)->delete();

    return $this->redirect(['index']);
}
Run Code Online (Sandbox Code Playgroud)