PHP traversal folder and file classes and processing class usage examples, _php tutorial

Source: Internet
Author: User
Tags learn php learn php programming

PHP traversal folder and file classes and processing class usage instances,


The example of this paper describes the PHP traversal folder and file classes and processing class usage, very practical value. Share to everyone for your reference. Here's how:

The FindFile.class.php class file is used to traverse the directory file, with the following code:

<?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 func       tion 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}/* Traverse files and folders * @param String $spath folder path * @param int $depth current Folder depth */Private function Traversin G ($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->traversing ($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 is used to clear the BOM of the Utf8+bom file, which is the first three bytes 0xEF 0xBB 0xBF, inheriting the FindFile class, with the following code:

<?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 ($this->c    Heck_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 function Che     Ck_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;  }}}?>

Example of removing UTF8 BOM header demo traversal file:

<?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 will be helpful for you to learn PHP programming.




http://www.bkjia.com/PHPjc/882900.html www.bkjia.com true http://www.bkjia.com/PHPjc/882900.html techarticle PHP Traversal folder and file class and processing class usage instances, this article describes the PHP traversal folder and file classes and processing class usage, very practical value. Share for everyone to join us ...

  • 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.