PHP traversal folder and file classes and processing classes

Source: Internet
Author: User
Tags ord require

FindFile.class.php

Used to traverse a directory file

<?php/** Traverse folder and File class * date:2013-03-21 * Author:fdipzone * ver:1.0/class findfile{    Public $files = Array ();        Store the traversed file protected $maxdepth;  Search depth, 0 means no Limit/* Traverse file and folder * @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); 
    Traverse}/* Traverse file and folder * @param String $spath folder path * @param int $depth current folder depth * * Private function traversing ($spath, $depth =1) {if ($handle = Opendir ($spath)) {while ($f Ile=readdir ($handle))!==false {if ($file!= '. ' && $file!= ' ... '){$curfile = $spath. '  
      
                    /'. $file;  
                            if (Is_dir ($curfile)) {//dir if ($this->maxdepth==0 | | $depth < $this->maxdepth) {//Judge depth  
                        $this->traversing ($curfile, $depth + 1);  
                    }}else{//File $this->handle ($curfile);  
        }} closedir ($handle);  
        /** Processing File Method * @param String $file file path */protected function handle ($file) {  
    Array_push ($this->files, $file); }}?>

UnsetBom.class.php is used to clear the BOM for the Utf8+bom file, that is, the first three bytes 0xEF 0xBB 0xBF, inheriting the FindFile class

<?php/** traverse All files, clear Utf8+bom 0xEF 0xBB 0xBF * date:2013-03-21 * author:fdipzone * ver:1.0 * \ Class  
    Unsetbom extends findfile{private $filetype = Array ();//file type/initialization to be processed  
        The Public function __construct ($filetype =array ()) {if ($filetype) {$this->filetype = $filetype; /** Rewrite FindFile handle method * @param String $file File path * * Protected fun  
            Ction 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 if 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 &Amp;& 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);
    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

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

Author: csdn blog proud Snow star Maple

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/PHP/

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.