Batch to BOM Head traverse directory and sub-files, folder PHP source code

Source: Internet
Author: User
Tags php source code

Any PHP file, replace the last line with your own directory

<?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( ‘您的目录‘ );

Batch to BOM Head traverse directory and sub-files, folder PHP source code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.