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

來源:互聯網
上載者:User

標籤:副檔名   sub   ring   目錄   files   開啟檔案   family   get   取出   

任意php檔案,把最後一行替換成自己的目錄 即可

<?php

class KillBom{    public static $m_Ext = [‘txt‘, ‘php‘, ‘js‘, ‘css‘];//檢查的副檔名     /**     * 傳入一個任意檔案 ,自動區分定義的副檔名,然後過濾bom     * @param string $file     * @return boolean     */    public static  function killBomByFile($file)    {        $ext = pathinfo($file,PATHINFO_EXTENSION);//擷取一個檔案 的副檔名         if (in_array($ext, self::$m_Ext) and  is_file($file))//允許被替換,而且是個檔案 (不是目錄 )        {            $content = file_get_contents($file);//取出檔案 詳情                     if (substr($content, 0, 3) == chr(0xEF) . chr(0xBB) . chr(0xBF))//EFBBBF 檢查bom            {                return file_put_contents($file, substr($content, 3)) > 0;//清除bom並寫入檔案             }        }        return false;    }    /**     * 遍曆擷取子目錄 及檔案夾     * @param string $dir     * @return string[]     */    public static function  getFileListByDir($dir)    {        $dir_handle = opendir($dir);//開啟檔案         $result = [];//存結果        while ($file = readdir($dir_handle))//不斷讀取目錄         {            if ($file != ‘.‘ and $file != ‘..‘)//不是本,上級目錄             {                $file = $dir . DIRECTORY_SEPARATOR . $file;//組裝成檔案的絕對路徑                if (is_dir($file))//是目錄 的話                {                    $result = array_merge($result ,  self::getFileListByDir($file));//遞迴併合並結果                } else {                    $result[] = $file;//記錄結果                }            }        }        return $result;//返回結果    }    /**     * 清空目錄 下所有的bom標頭檔     * @param string $dir     */    public static function killDir($dir)    {        $files = self::getFileListByDir($dir);//先找到所有檔案         foreach ($files as $file)//遍曆        {            if (!self::killBomByFile($file))//幹掉            {                echo $file .‘ -> no bom! <br>‘.chr(13);//結果            } else {                echo $file . ‘ -> bom is killed! <br>‘.chr(13);//結果            }        }             }}//把下面這行替換成自己的目錄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.