This article gives you the content is about how to delete the database in the THINKPHP5 and upload to the server at the same time, there is a certain reference value, there is a need for friends to refer to, I hope you have some help.
How to delete the database and the files uploaded to the server at the same time in TP5:
The methods defined in the model are as follows
<?phpnamespace app\admin\model;use think\model;class Pic extends model{public function unlink ($path) {return is_file ($path) && unlink ($path);}}
Then call your method in the controller, it is recommended to $path the absolute path in the controller,
Introduce the spatial class element in the controller:
Use Think\file;use app\admin\model\pic as picmodel;//prevent class name repetition error, an alias
Then instantiate in the method and invoke the method:
public Function del () {$id =input (' id ');//Returns the result for the obtained ID $lu Nbotu=db (' Lunbotu ')->find ($id);//Get a piece of data where you can dump () data, return the result to an array, access the field where the file is stored, and here is the pic $path = '. /public/static/uploads/'. $lunbotu [' Pic '];//define the path to the file $unlink =new Picmodel ();//Instantiate if ($unlink->unlink ($path) && db (' Lunbotu ')->delete (input (' id '))) {return redirect (' pic ');//redirect to the interface you want}else{$this->error (' Delete Carousel failed ');} }