PHP traverses folder and file class and processing class usage instances ,. PHP traversal examples of folder and file class and processing class usage. this example describes PHP traversal of folder and file class and processing class usage, which is very useful. I would like to share with you some examples of PHP folder and file class traversal and processing class usage,
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-> files = 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) {// dir if ($ 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 ($ file) {// utf8 + bom $ this-> clear_utf8bom ($ file); // clear array_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 S Tring $ 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.
Examples in this article describes the usage of PHP traversal folders and file classes and processing classes, which is very useful. I will share it with you for your reference...