1. 程式人生 > >ThinkPHP清除快取/刪除資料夾下所有檔案

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<$count;$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方式提交

<script type="text/javascript" src="__PUBLIC__/scripts/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
$(function(){
    $('#button').click(function(){
        if(confirm("確認要清除模版快取目錄?")) {
        var $type=$('#type').val();
        var $mess=$('#mess');
        $.getJSON('__URL__/cache',{type:$type},function(json){
            if(json.status==1){
            $mess.slideDown(3000,function(){
                $mess.css('color:red');   
            }).html(json.info);
            }else{
                $mess.slideDown(3000,function(){
                    $mess.css('color:red');   
                }).html(json.info);
            }
        });
         }else{
                return false;
            }   
    });
});      
</script>

<form action="" method="get">
                      <table border="0" cellpadding="2" cellspacing="1" style="width:100%">

                       <tr>

                           //關鍵就是這個隱藏域的預設值 就是你要刪除資料夾裡面內容的資料夾的名

                           <input type="hidden" name="type" id="type" class="text" style="width:50px" value="Cache" />
                        <td nowrap align="left"><input type="button" id="button" class="text" style="width:100px;height:50px;" value="點選清除模版快取目錄" /></td>
                        <td id="mess"></td>
                      </tr>
                      </table>

                </form>

//一鍵清除所有

<form action="" method="get">
                      <table border="0" cellpadding="2" cellspacing="1" style="width:100%">
                       <tr>

                            //預設值是幾個資料夾的名字連起來 提交方式和上面的一樣
                           <input type="hidden" name="type" id="type" class="text" style="width:50px" value="Cache-Data-Temp-Logs" />
                        <td nowrap align="left"><input type="button" id="button" class="text" style="width:100px;height:50px;" value="點選一鍵清除所有" /></td>
                          <td id="mess"></td>
                      </tr>
                      </table>
                </form>