For the PHP directory and file traversal class, you are welcome to shoot bricks /****************************** **************************************** * *********** directory and file traversal class ** -------------------------- PHP directory and file traversal class welcome everyone to make a brick
/*************************************** **************************************** * ** Directory and file traversal class ** ----------------------------------------------- * Basic version, even a small blog will gradually improve these classes based on time and usage * In addition, we recommend that you use the InitPHP 2.0 domestic PHP development framework link: http://www.initphp.com * ----------------------------------------- * $ Author: liuxinming * $ Dtime: 2012-06-23 *************************************** **************************************** * ***/class getfiles {/** directory */private $ path; /** type * @ param $ typeimg image format * @ param $ typeflash * @ param $ typesp video format * @ param $ typetext text format * @ param $ typeplay playback media format * according you can add the format */private letter typeimg}',.jpg, .gif, .png, .bmp, .jpeg, '; Private Keys typeflash}',.swf ,. flv ,. dcr, '; private struct typespath' ,.wmv,.asf,.wma ,. asx ,. mid ,. midi, '; private parameter typetext}', .txt,.doc ,. exl, '; private character typeplay}',.rm,.ra,.rmvb,.mp4,.mov,.avi,.wav ,. ram ,. mpg ,. mpeg, ';/*** constructor */function _ construct ($ path) {$ this-> path = $ path ;} /*** get the file extension */function GetFiletype ($ filename) {$ filer = explode (". ", $ filename); $ count = count ($ filer)-1; return strtolower (". ". $ this -> RepGetFiletype ($ filer [$ count]);}/*****/function RepGetFiletype ($ filetype) {$ filetype = str_replace ('| ','_', $ filetype); $ filetype = str_replace (',', '_', $ filetype); $ filetype = str_replace ('. ',' _ ', $ filetype); return $ filetype;}/*** file size format conversion * @ param string $ filesize */function pathFilesize ($ size) {if ($ size> = 1024*1024) {// MB // The function uses a thousand-bit grouping to format the number $ filesize = number_format ($ size/(1024*1024), 2, '. ',''). "MB";} el Se if ($ size> = 1024) {// KB $ filesize = number_format ($ size/1024,2 ,'. ',''). "KB";} else {$ filesize = $ size. "Bytes";} return $ filesize;}/*** traverse the directory and file and echo the output **/function getfiles () {$ path = $ this-> path; // check whether the specified file or directory exists if (! Is_dir ($ path) continue; // open the directory handle $ dir = opendir ($ path); while ($ file = readdir ($ dir ))! = False) {if ($ file = '.' | $ file = '..') {continue;} if ($ file! = '.' | $ File! = '.. ') {// Directory $ pathfile = $ path. '/'. $ file; if (is_dir ($ pathfile) {// output directory of this branch $ filename = $ file; // release time $ filetime = @ filemtime ($ pathfile ); $ filetime = date ("Y-m-d H: I: s", $ filetime); $ filesize = '<Directory>'; $ filetype = 'folder ';} else {// output file of this branch $ filename = $ file; $ ftype = $ this-> GetFiletype ($ file ); // release time $ filetime = @ filemtime ($ pathfile); $ filetime = date ("Y-m-d H: I: s", $ filetime ); // File Size $ fsize = @ filesize ($ pathfile); $ filesize = $ this-> pathFilesize ($ fsize ); // if (strstr ($ this-> typeimg ,','. $ ftype. ',') {$ filetype = 'image';} elseif (strstr ($ this-> typeflash ,','. $ ftype. ',') {$ filetype = 'Flash';} elseif (strstr ($ this-> typesp ,','. $ ftype. ',') | strstr ($ typeplay ,','. $ ftype. ',') {$ filetype = 'Video';} elseif (strstr ($ this-> typetext ,','. $ ftype. ',') {$ filetype = 'text';} else {$ filetype = 'upload ';}} $ table = '<table width = "100%" border = "0" align = "center" cellpadding = "3" cellspacing = "1"> '; $ tr = '<tr>'; $ tr1 = '</tr>'; $ table1 = '</table>'; $ td = $ tr. '& lt; td width = 50% & gt '. $ filename. '</td> <td width = 20%> '. $ filetype. '</td> '. '</td> <td width = 20%> '. $ filesize. '</td> '. $ tr1; echo $ return = $ table. $ td. $ table1 ;}else {continue ;}}}}
Usage:
$ Path = '/var/www/lxm/ecms ';
$ Files = new getfiless ($ path );
$ Files-> getfiles ();
After running: