About PHP Traversal folders and file classes and processing classes

Source: Internet
Author: User
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-&gt    ; files, $file); }}?>

UnsetBom.class.php

<?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);? >

This article explains about the PHP traversal folder and file classes and processing classes related content, more relevant knowledge, please pay attention to the PHP Chinese web.

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.