PHP traversal of folders and file classes and processing class usage examples

Source: Internet
Author: User
Tags learn php programming
This article mainly introduces PHP folder traversal and file class and processing class usage examples, including file and folder traversal and utf8 bom header clearing methods, which are very practical. For more information, see

This article mainly introduces PHP folder traversal and file class and processing class usage examples, including file and folder traversal and utf8 bom header clearing methods, which are very practical. For more information, see

This article describes the usage of PHP to traverse folders, file classes, and processing classes, which is of great practical value. Share it with you for your reference. The specific method is as follows:

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

<? Php/** traverse folders and file Classes * Date: 2013-03-21 * Author: fdipzone * Ver: 1.0 */class FindFile {public $ files = array (); // stores the file protected $ maxdepth; // search depth, 0 indicates no limit./* traverse files and folders * @ param String $ spath folder path * @ param int $ maxdepth search depth. By 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-> fi Les = array (); $ this-> traversing ($ spath ); // traverse}/* 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) {// dirif ($ this-> maxdepth = 0 | $ depth <$ this-> maxdepth) {// determine the depth $ this-> traversing ($ curfile, $ depth + 1) ;}} else {// file $ this-> handle ($ curfile );}}} closedir ($ handle) ;}}/** file Processing Method * @ param String $ 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, that is, the first three bytes 0xEF 0xBB 0xBF, inherit the 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 (); // file type to be processed // initialize public function _ construct ($ filetype = array ()) {if ($ filetype) {$ this-> filetype = $ filetype ;}} /** override FindFile handle Method * @ param String $ file path */protected function handle ($ file) {if ($ this-> check_ext ($ file) & $ this-> check_utf8bom ($ fi Le) {// utf8 + bom $ this-> clear_utf8bom ($ file); // cleararray_push ($ this-> files, $ file ); // save log}/** check whether the file is utf8 + bom * @ param String $ file * @ return boolean */private function check_utf8bom ($ file) {$ content = file_get_contents ($ file); return ord (substr ($ content,) ===0xef & ord (substr ($ content )) === 0xBB & ord (substr ($ content, 2, 1) ===0xbf;}/** clear utf8 + bom * @ param String $ file path */private Function clear_utf8bom ($ file) {$ content = file_get_contents ($ file); file_put_contents ($ file, substr ($ content, 3), true ); // turn around three bytes}/** check the file type * @ param String $ 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 file traversal example for removing the utf8 bom header:

<? 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 help you learn PHP programming.

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.