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

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

不言
不言Original
2018-08-27 17:37:424651browse

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("删除失败!");
        }
    }

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!

Statement:
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