PHP directory and file traversal class Welcome to shoot bricks.
/********************************************************************************* * Directory and File traversal class *------------------- ------------------------* Primary version, my little blog follow-up based on time and use will gradually improve these classes * Also recommended you can use 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 $typ EIMG Picture Format * @param $typeflash * @param $typesp Video format * @param $typetext text Format * @param $typeplay Play Media Format * You can add your own format to your needs */Private $typeimg = ',. Jpg,.gif,.png,.bmp,.jpeg, '; Private $typeflash = ',. SWF,.FLV,.DCR, '; Private $typesp = ',. Wmv,.asf,.wma,.mp3,.asx,.mid,.midi, '; Private $typetext = ',. Txt,.doc,.exl, '; Private $typeplay = ',. Rm,.ra,.rmvb,.mp4,.mov,.avi,.wav,.ram,.mpg,.mpeg, '; /** * Constructor */function __construct ($path) {$this->path= $path; }/** * Gets 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//function is formatted by thousands grouping Digital $filesize =number_format ($size/(1024*1024), 2, '. ', '). " MB "; }else if ($size >=1024) {//kb $filesize =number_format ($size/1024,2, '. ', '). " KB "; }else{$filesize = $size. " Bytes "; } return $filesize; }/** * Traverse directory and file and echo output * */function GetFiles () {$path = $this->path; Checks if 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)) {//This branch output directory $filename = $file;//Publish Time $filetime = @filemtime ($pathfile); $filetime =date ("y-m-d h:i:s", $filetime); $filesize = ' < directory > '; $filetype = ' folder '; }else{//This branch output file $filename = $file; $ftype = $this->getfiletype ($file);//Publish Time $filetime= @filemtime ($pathfile); $ Filetime=date ("y-m-d h:i:s", $filetime);//File Size $fsize= @filesize ($pathfile); $filesize = $this->pathfilesize ($ Fsize); File type if (Strstr ($this->typeimg, ', '. $ftype. ', ')) {$filetype = ' picture ';} 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 = ' attachment ';}} $table = ' <table width= ' 100% "border=" 0 "align=" center "cellpadding=" 3 "cellspacing=" 1 "> '; $tr = ' <tr> '; $TR 1 = ' </tr> '; $table 1= ' </table> '; $td = $tr. ' <td width=50%> '. $filename. ' </td><td width=20%> '. $filetype. ' </td> '. ' </td><td width=20%> '. $filesize. ' </td> '. $tr 1;echo $return = $table. $td. $table 1; }else{continue;} } } }
How to use:
$path = '/var/www/lxm/ecms ';
$files =new getfiless ($path);
$files->getfiles ();
After running: