How to delete a database in thinkphp5 and upload files to the server at the same time

Source: Internet
Author: User
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 ');} }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.