[Comment] write an OOP object-oriented class. function: scan the entire site folder!
Function:Scan all specified types of files on the entire site and collect their paths to the database. (Used to filter out files with a size of 0)
Completed:I use OOP to write, but I always feel a lot of deficiencies. In the past, basically no OOP was used, and all processes were involved. Comment on this "class ".
Note:In fact, there is a function that is not implemented, that is, it supports scanning the entire site from the specified directory.
Code:
Timestart = microtime (true); $ this-> webroot = ROOT_PATH; if (substr ($ this-> webroot,-1) = '/') $ this-> webroot = substr ($ this-> webroot, 0,-1); $ this-> db = $ GLOBALS ['DB']; $ this-> tablename = 'mytools _ filescan'; $ this-> handlepath = dirname (_ FILE __). '/data/filescan. log'; $ this-> outMessage = array (); $ this-> fieldsarr = array ('realpath', 'realpath _ md5', 'filepath ', 'is _ file', 'is _ dir', 'fileext ', 'filesi Z', 'filemtime', 'filename', 'is _ list', 'is _ detail', 'rec _ id', 'rec _ type',); if (! $ This-> TableHas ()&&! $ This-> TableCreate ()&&! $ This-> TableHas () {$ this-> ShutDown ('Table creation failed! ') ;}$ This-> currInfos = array (); $ this-> allowFiletype = array (); $ this-> setMessage (' initialization is complete! ');} Public function setAllowFiletype ($ fileext) {if (! In_array ($ fileext, $ this-> allowFiletype) $ this-> allowFiletype [] = $ fileext;} public function isAllowFiletype ($ ext) {if (! Empty ($ this-> allowFiletype )&&! In_array ($ ext, $ this-> allowFiletype) {return FALSE;} return TRUE;} public function setHandle ($ path = '') {file_put_contents ($ this-> handlepath, $ path);} public function getHandle () {return file_get_contents ($ this-> handlepath);} public function setMessage ($ message) {$ this-> outMessage [] = date ('H: I: s '). $ message;} public function ShutDown ($ message) {$ this-> setMessage ($ message); $ this-> setMessage (sprintf ('
Total time consumed: %. 6f seconds! Scanned file% D(Microtime (true)-$ this-> timestart), $ this-> filecount); foreach ($ this-> outMessage as $ k => $ v) {echo ''. $ v.'
';} Exit ();} public function resetAll () {$ this-> TableTruncate (); $ this-> setHandle (); $ this-> setMessage ('reset all! ');} Public function AddRow ($ infos) {$ setsql = $ comma = ''; foreach ($ this-> fieldsarr as $ k) {if (isset ($ infos [$ k]) {$ setsql. = $ comma. "'$ k' = '". addslashes (htmlspecialchars ($ infos [$ k]). "'"; $ comma =', ';} if (! Empty ($ setsql) {$ this-> db-> query ("replace ". $ this-> tablename. "SET {$ setsql}") ;}} public function DirIsBefore ($ path1, $ path2) {$ path1 = strtolower ($ path1); $ path2 = strtolower ($ path2 ); if ($ path1 = $ path2) return TRUE; $ arr1 = explode ('/', $ path1); $ arr2 = explode ('/', $ path2 ); $ arr1Len = count ($ arr1); $ arr2Len = count ($ arr2); $ I = 0; while (true) {if ($ I> $ arr1Len & $ I> $ arr2Len) break; if ($ Arr1 [$ I]! = $ Arr2 [$ I]) {return $ this-> CompareBefore ($ arr1 [$ I], $ arr2 [$ I]);} $ I ++ ;} return FALSE;} // text comparison public function CompareBefore ($ str1, $ str2) {if ($ str1 ==$ str2) return TRUE; $ str1Length = strlen ($ str1); $ str2Length = strlen ($ str2); $ I = 0; while (true) {if ($ I >$ str1Length & $ I >$ str2Length) break; $ k1 = $ str1 {$ I}; $ k2 = $ str2 {$ I }; if ($ k1! = $ K2) {return (ord ($ k1) <ord ($ k2 ))? TRUE: FALSE;} $ I ++;} return FALSE;} public function Scan ($ path = '') {// by default, if (empty ($ path) $ path = $ this-> webroot; $ this-> setHandle ($ path) is scanned from the root directory ); if (file_exists ($ path) {if (is_dir ($ path) {$ this-> setMessage ('positive scan :'. $ path. '... '); $ dh = opendir ($ path); if ($ dh) {while ($ file = readdir ($ dh ))! = FALSE) {if ($ file! = '.' & $ File! = '.. ') {$ This-> Scan ($ path. '/'. $ file) ;}}} elseif (is_file ($ path) {$ this-> Record ($ path); $ this-> filecount ++ ;} else {return ;}} public function GetFileInfos ($ filepath) {$ arr = array (); if (file_exists ($ filepath) {if (is_file ($ filepath )) {$ a1 = pathinfo ($ filepath); // filter type if (! $ This-> isAllowFiletype ($ a1 ['extension']) {return;} $ arr ['is _ file'] = 1; $ arr ['is _ dir'] = 0; $ arr ['fileext '] = $ a1 ['extension']; $ arr ['filename'] = $ a1 ['filename']. '. '. $ a1 ['extension']; $ arr ['filemtime'] = filemtime ($ filepath); $ arr ['filesize'] = filesize ($ filepath ); // product list page if (preg_match ('/\/product \/(\ d +) \/index (-\ d + )? \. Html? $/I ', $ filepath, $ matches) {$ arr ['rec _ id'] = intval ($ matches [1]); $ arr ['rec _ type'] = 'Gl ';} // elseif (preg_match ('/\/product \/[^ \/] + \/(\ d + )\. html? $/I ', $ filepath, $ matches) {$ arr ['rec _ id'] = intval ($ matches [1]); $ arr ['rec _ type'] = 'gd '; }} else {$ arr ['is _ file'] = 0; $ arr ['is _ dir'] = 1;} $ arr ['filepath'] = $ filepath; $ arr ['realpath'] = realpath ($ filepath ); $ arr ['realpath _ md5'] = md5 ($ arr ['realpath']); // prevents repeated data writes} return $ arr;} public function fileext ($ filepath) {return substr ($ filepath, strrchr ($ filepath ,'. ');} public function Record ($ f Ilepath) {$ infos = $ this-> GetFileInfos ($ filepath); if (! Empty ($ infos) $ this-> AddRow ($ infos); return TRUE;} public function TableCreate () {$ SQL = <
Db-> query ($ SQL); return TRUE;} public function TableTruncate () {$ this-> db-> query ('truncate '. $ this-> tablename);} public function TableDrop () {$ this-> db-> query ("drop table if exists ". $ this-> tablename);} public function TableHas () {return $ this-> db-> getOne ('Show tables like \''. $ this-> tablename. '\ '') = $ this-> tablename? TRUE: FALSE;} public function Summary () {$ size1M = 1024*1024; $ sqlarr = array ('total file count: '=> '1',' greater than 1 M: '=> 'filesize> '. $ size1M, 'Size: 0: '=> 'filesize = 0 ','
Dangerous files'=> "Fileext REGEXP' exe | dll | zip | rar '",); $ message = $ comma = ''; foreach ($ sqlarr as $ k => $ v) {$ r = $ this-> db-> getOne ("select count (*) FROM ". $ this-> tablename. "WHERE ". $ v); $ message. = $ comma. $ k. ''. $ r. ''; $ comma = ',';} $ this-> setMessage ($ message);} public function num_bitunit ($ num) {$ bitunit = array ('B ', 'KB', 'mb', 'GB'); for ($ key = 0, $ count = count ($ bitunit); $ key <$ count; $ key ++) {if ($ num> = pow (2, 10 * $ key)-1) // 1024B is displayed as 1KB {$ num_bitunit_str = (ceil ($ num/pow (2, 10 * $ key) * 100)/100 ). "$ bitunit [$ key]" ;}} return $ num_bitunit_str;} public function real_url ($ filepath) {return str_replace ($ this-> webroot ,'', $ filepath) ;}public function _ destruct () {clearstatcache ();}}
Code:
If (! Is_object ($ fscan) $ fscan = new FileScan (); $ fscan-> resetAll (); $ fscan-> setAllowFiletype ('htm '); $ fscan-> setAllowFiletype ('html'); $ fscan-> Scan (ROOT_PATH. 'product'); $ fscan-> Summary (); $ fscan-> ShutDown ('scan finished! Click here to view the results! Script window. location. hash = "# bottom"; script ');
Defects:The whole site is not scanned from the specified directory. Function DirIsBefore () is added in the Directory comparison. However, if the current start directory is "/zzzzzz.zip" and the folder "/aaaaa/aaaaaa/ddddd/" is displayed, will still scan ~ Comparison is not completely implemented.
Let me see if this meets the OOP specifications? Deficiencies in writing
Reply to discussion (solution)
Your class cannot run independently. this is the biggest problem.
This is not like a class. it is like changing process-oriented to object-oriented (single instance)