批量去BOM頭 遍曆目錄及子檔案,檔案夾 PHP源碼

來源:互聯網
上載者:User

標籤:static   檔案   歸併   param   als   擴充   handle   ram   sub   

 1 <?php 2 class KillBom 3 { 4     public static $m_Ext = [‘txt‘, ‘php‘, ‘js‘, ‘css‘];//檢查的副檔名  5     /** 6      * 傳入一個任意檔案 ,自動區分定義的副檔名,然後過濾bom 7      * @param string $file 8      * @return boolean 9      */10     public static  function killBomByFile($file)11     {12         $ext = pathinfo($file,PATHINFO_EXTENSION);//擷取一個檔案 的副檔名 13         if (in_array($ext, self::$m_Ext) and  is_file($file))//允許被替換,而且是個檔案 (不是目錄 )14         {15             $content = file_get_contents($file);//取出檔案 詳情16          17             if (substr($content, 0, 3) == chr(0xEF) . chr(0xBB) . chr(0xBF))//EFBBBF 檢查bom18             {19                 return file_put_contents($file, substr($content, 3)) > 0;//清除bom並寫入檔案 20             }21         }22         return false;23     }24     /**25      * 遍曆擷取子目錄 及檔案夾26      * @param string $dir27      * @return string[]28      */29     public static function  getFileListByDir($dir)30     {31         $dir_handle = opendir($dir);//開啟檔案 32         $result = [];//存結果33         while ($file = readdir($dir_handle))//不斷讀取目錄 34         {35             if ($file != ‘.‘ and $file != ‘..‘)//不是本,上級目錄 36             {37                 $file = $dir . DIRECTORY_SEPARATOR . $file;//組裝成檔案的絕對路徑38                 if (is_dir($file))//是目錄 的話39                 {40                     $result = array_merge($result ,  self::getFileListByDir($file));//遞迴併合並結果41                 } else {42                     $result[] = $file;//記錄結果43                 }44             }45         }46         return $result;//返回結果47     }48     /**49      * 清空目錄 下所有的bom標頭檔50      * @param string $dir51      */52     public static function killDir($dir)53     {54         $files = self::getFileListByDir($dir);//先找到所有檔案 55         foreach ($files as $file)//遍曆56         {57             if (!self::killBomByFile($file))//幹掉58             {59                 echo $file .‘ -> no bom! <br>‘.chr(13);//結果60             } else {61                 echo $file . ‘ -> bom is killed! <br>‘.chr(13);//結果62             }63         }64          65     }66 }67 //把下面這行替換成自己的目錄68 KillBom::killDir(‘您的目錄‘);

 

批量去BOM頭 遍曆目錄及子檔案,檔案夾 PHP源碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.