Home Backend Development PHP Tutorial ThinkPHP清除缓存/删除文件夹上所有文件

ThinkPHP清除缓存/删除文件夹上所有文件

Jun 13, 2016 pm 01:15 PM
gt lt nbsp quot type

ThinkPHP清除缓存/删除文件夹下所有文件

今天做了一个ThinkPHP清除缓存功能,在网上开了一下有一个比较简单的写法但是那个是ThinkPHP内置的一个类,我找了半天没找到这个类,气死我了  于是就用php删除文件夹下所有文件这个方法来达到清除缓缓存的的功能,废话不多说粘上代码:

/*此方法为公共方法用来删除某个文件夹下的所有文件
     * $path为文件的路径
     * $fileName文件夹名称
     * */
    public function rmFile($path,$fileName){
        //去除空格
        $path = preg_replace('/(\/){2,}|{\\\}{1,}/','/',$path);    
        //得到完整目录    
        $path.= $fileName;
        //判断此文件是否为一个文件目录
        if(is_dir($path)){
            //打开文件
            if ($dh = opendir($path)){
                //遍历文件目录名称
                   while (($file = readdir($dh)) != false){
                       //逐一进行删除
                       unlink($path.'\\'.$file);
                       }
                       //关闭文件
                      closedir($dh);
                }    
            }
    }

//一对一删除缓存
    public function cache(){

        //前台用ajax get方式进行提交的,这里是先判断一下
        if($_GET['type']){

             //获取提交过来的值(也就是要删除的文件夹名称)

            $type=$_GET['type'];

            //获取文件的绝对路径

            $abs_dir=dirname(dirname(dirname(dirname(__FILE__))));

            //与要删除的文件夹里的文件进行组合
            $pa=$abs_dir.'\Admin\Runtime\\';

           //调用上面写好的方法

            $this->rmFile($pa,$type);

            //返回提示信息
            $this->ajaxReturn(1,'清除成功',1);
        }else{
            $this->display();
        }
    }

//一键清除所有缓存
    public function allrun(){

        ////前台用ajax get方式进行提交的,这里是先判断一下
        if($_GET['type']){

          //得到传递过来的值
            $type=$_GET['type'];

            //将传递过来的值进行切割,我是已“-”进行切割的
            $name=explode('-', $type);

            //得到切割的条数,便于下面循环

            $count=count($name);

           //循环调用上面的方法

            for ($i=0;$i

                //得到文件的绝对路径

                $abs_dir=dirname(dirname(dirname(dirname(__FILE__))));

                //组合路径
                $pa=$abs_dir.'\Admin\Runtime\\';

                //调用删除文件夹下所有文件的方法
                $this->rmFile($pa,$name[$i]);    
            }

           //给出提示信息

            $this->ajaxReturn(1,'清除成功',1);
        }else{
            $this->display();
        }
    }

前台页面很简单 在这里就给出重要的代码

html文件

//这个是ajax get方式提交




                     

                      

                           //关键就是这个隐藏域的默认值 就是你要删除文件夹里面内容的文件夹的名

                           
                       
                       
                     
                     

                

//一键清除所有


                     
                      

                            //默认值是几个文件夹的名字连起来 提交方式和上面的一样
                           
                       


                          
                     
                     

                


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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Solution: Your organization requires you to change your PIN Solution: Your organization requires you to change your PIN Oct 04, 2023 pm 05:45 PM

Solution: Your organization requires you to change your PIN

How to adjust window border settings on Windows 11: Change color and size How to adjust window border settings on Windows 11: Change color and size Sep 22, 2023 am 11:37 AM

How to adjust window border settings on Windows 11: Change color and size

How to change title bar color on Windows 11? How to change title bar color on Windows 11? Sep 14, 2023 pm 03:33 PM

How to change title bar color on Windows 11?

How to enable or disable taskbar thumbnail previews on Windows 11 How to enable or disable taskbar thumbnail previews on Windows 11 Sep 15, 2023 pm 03:57 PM

How to enable or disable taskbar thumbnail previews on Windows 11

OOBELANGUAGE Error Problems in Windows 11/10 Repair OOBELANGUAGE Error Problems in Windows 11/10 Repair Jul 16, 2023 pm 03:29 PM

OOBELANGUAGE Error Problems in Windows 11/10 Repair

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

What are the differences between Huawei GT3 Pro and GT4?

Display scaling guide on Windows 11 Display scaling guide on Windows 11 Sep 19, 2023 pm 06:45 PM

Display scaling guide on Windows 11

10 Ways to Adjust Brightness on Windows 11 10 Ways to Adjust Brightness on Windows 11 Dec 18, 2023 pm 02:21 PM

10 Ways to Adjust Brightness on Windows 11

See all articles