PHP Traversing Folder and File Classes and Processing Class Usage Examples

  • 2021-07-18 07:28:48
  • OfStack

This article describes the example of PHP traversal folder and file class and processing class usage, very practical value. Share it for your reference. The specific methods are as follows:

The FindFile. class. php class file is used to traverse directory files, as follows:


<?php 
/**  Traversing folders and file classes  
*  Date:  2013-03-21 
*  Author: fdipzone 
*  Ver:  1.0 
*/ 
class FindFile{ 
 
  public $files = array();  //  Store traversed files  
  protected $maxdepth;    //  Search depth ,0 Indicates that there are no restrictions  
 
  /*  Traversing files and folders  
  *  @param String $spath    Folder path  
  *  @param int  $maxdepth  Search depth , Default search all  
  */ 
  public function process($spath, $maxdepth=0){ 
    if(isset($maxdepth) && is_numeric($maxdepth) && $maxdepth>0){ 
      $this->maxdepth = $maxdepth; 
    }else{ 
      $this->maxdepth = 0; 
    } 
    $this->files = array(); 
    $this->traversing($spath); //  Traversal  
  } 
 
  /*  Traversing files and folders  
  *  @param String $spath  Folder path  
  *  @param int  $depth  Current folder depth  
  */ 
  private function traversing($spath, $depth=1){ 
    if($handle = opendir($spath)){ 
      while(($file=readdir($handle))!==false){ 
        if($file!='.' && $file!='..'){ 
          $curfile = $spath.'/'.$file; 
 
          if(is_dir($curfile)){ // dir 
            if($this->maxdepth==0 || $depth<$this->maxdepth){ //  Depth of judgment  
              $this->traversing($curfile, $depth+1); 
            } 
          }else{ // file 
            $this->handle($curfile); 
          } 
        } 
      } 
      closedir($handle); 
    } 
  } 
 
  /**  Method of processing files  
  * @param String $file  File path  
  */ 
  protected function handle($file){ 
    array_push($this->files, $file); 
  } 
} 
?> 

UnsetBom. class. php is used to clear bom of utf8+bom file, that is, the first three bytes 0xEF 0xBB 0xBF inherit FindFile class, the specific code is as follows:


<?php 
/**  Traverse all files and clear utf8+bom 0xEF 0xBB 0xBF 
*  Date:  2013-03-21 
*  Author: fdipzone 
*  Ver:  1.0 
*/ 
class UnsetBom extends FindFile{ 
 
  private $filetype = array(); //  Types of files to be processed  
 
  //  Initialization  
  public function __construct($filetype=array()){ 
    if($filetype){ 
      $this->filetype = $filetype; 
    } 
  } 
 
  /**  Rewrite FindFile handle Method  
  *  @param String $file  File path  
  */ 
  protected function handle($file){ 
    if($this->check_ext($file) && $this->check_utf8bom($file)){ // utf8+bom 
      $this->clear_utf8bom($file);    // clear 
      array_push($this->files, $file);  // save log 
    } 
  } 
 
  /**  Check whether the file is utf8+bom 
  *  @param String $file  File path  
  *  @return boolean 
  */ 
  private function check_utf8bom($file){ 
    $content = file_get_contents($file); 
    return ord(substr($content,0,1))===0xEF && ord(substr($content,1,1))===0xBB && ord(substr($content,2,1))===0xBF; 
  } 
 
  /**  Clear utf8+bom 
  *  @param String $file  File path  
  */ 
  private function clear_utf8bom($file){ 
    $content = file_get_contents($file); 
    file_put_contents($file, substr($content,3), true); //  To turn around 3 Bytes  
  } 
 
  /**  Check the file type  
  *  @param String $file  File path  
  *  @return boolean 
  */ 
  private function check_ext($file){ 
    $file_ext = strtolower(array_pop(explode('.',basename($file)))); 
    if(in_array($file_ext, $this->filetype)){ 
      return true; 
    }else{ 
      return false; 
    } 
  } 
} 
?> 

Remove utf8 bom header Demo traversal file example:


<?php 
require('FindFile.class.php'); 
require('UnsetBom.class.php'); 
 
$folder = dirname(__FILE__); 
 
$obj = new UnsetBom(array('php','css','js')); //  File type  
$obj->process($folder); 
 
print_r($obj->files); 
?>

I hope this article is helpful to everyone's study of PHP programming.


Related articles: