Home > Backend Development > PHP Tutorial > How to delete data and files together in tp5 (code)

How to delete data and files together in tp5 (code)

不言
Release: 2023-04-03 22:24:02
Original
4711 people have browsed it

The content of this article is about how tp5 can delete data and files together (code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

unlink is a method of deleting files, just pass in the file path

public function delete()
    {
        $article_id = input('article_id');
        if (intval($article_id) <= 0) {
            $this->error("参数错误!");
        }
        //删除文件
        $article_info = DB::name(&#39;Article&#39;)->where(array(&#39;article_id&#39; => $article_id))->find();
        $path = ROOT_PATH . &#39;Uploads&#39; . DS . $article_info[&#39;article_content&#39;];
        if (file_exists($path)) {
            unlink($path);//删除文件
        };
        //删除数据库
        $result = DB::name(&#39;Article&#39;)->where(array(&#39;article_id&#39; => $article_id))->delete();
        if ($result) {
            $this->success("成功删除" . $result . "个材料", &#39;Article/index&#39;, &#39;&#39;, 1);
        } else {
            $this->error("删除失败!");
        }
    }
Copy after login

Related recommendations:

php Delete data table and delete database

PHP Delete the first and last two lines of the txt file.

The above is the detailed content of How to delete data and files together in tp5 (code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
tp5
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Error running tp5 on tomcat
From 1970-01-01 08:00:00
0
0
0
tp5 video upload
From 1970-01-01 08:00:00
0
0
0
TP5 paging always has problems
From 1970-01-01 08:00:00
0
0
0
Set up that tp5.com
From 1970-01-01 08:00:00
0
0
0
tp5 search paging
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template