FindFile.class.php
Used to traverse directory files
<?php/** Traverse folder and File class * date:2013-03-21* author:fdipzone* ver:1.0*/class findfile{public $files = Array () ; Store traversed files protected $maxdepth; Search depth, 0 means no Limit/* Traverse 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) && $max depth>0) {$this->maxdepth = $maxdepth; }else{$this->maxdepth = 0; } $this->files = Array (); $this->traversing ($spath); Traversal}/* Traverse 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) {//judgment depth $this->trav Ersing ($curfile, $depth + 1); }}else{//File $this->handle ($curfile); }}} closedir ($handle); }}/** Process file method * @param String $file file path */protected function handle ($file) {Array_push ($this-> ; files, $file); }}?>
UnsetBom.class.php The BOM that clears the Utf8+bom file, which is the first three bytes 0xEF 0xBB 0xBF, inherits the FindFile class
<?php/** traverse all files, clear Utf8+bom 0xEF 0xBB 0xbf* date:2013-03-21* author:fdipzone* ver:1.0*/class Unsetbom exte NDS findfile{Private $filetype = Array ();//file type to be processed//Initialize public function __construct ($filetype =array ()) { if ($filetype) {$this->filetype = $filetype; }}/** override FindFile handle method * @param String $file file path */protected function handle ($file) {if ($t His->check_ext ($file) && $this->check_utf8bom ($file)) {//Utf8+bom $this->clear_utf8bom ($file) ; Clear Array_push ($this->files, $file); Save log}}/** check if file Utf8+bom * @param String $file File path * @return Boolean */Private F Unction Check_utf8bom ($file) {$content = file_get_contents ($file); Return Ord (substr ($content, 0,1)) ===0xef && Ord (substr ($content)) ===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); Remove the first three bytes}/** check 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; }}}?>
Demo unset UTF8 BOM
<?phprequire (' FindFile.class.php '); require (' UnsetBom.class.php '); $folder = DirName (__file__); $obj = new Unsetbom (Array (' PHP ', ' css ', ' JS '); File type $obj->process ($folder);p rint_r ($obj->files);? >
The above is the PHP Traversal folder and file class and processing class content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!