PHP indexes and file traversal

Source: Internet
Author: User
Tags filetime
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:



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.