"Please review" write an Oop object-oriented class, function: Scan the entire station folder!

Source: Internet
Author: User
Tags fscan
Features:Scans the entire station for all specified types of files, and collects their paths to the database. (Used to filter out files of size 0)
Complete:I use OOP to write, but I always feel a lot of shortcomings. In the past, basically not oop, all processes. Please review this "class".
remark:In fact, there is a function is not implemented, that is, the support from the specified directory to start scanning the entire station.
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 ', ' filesize ', ' filemtime ', ' filename ', ' is_list ', ' is_detail ', ' rec_id ', ' Rec_type ',        ; if (! $this->tablehas () &&! $this->tablecreate () &&! $this->tablehas ()) {$this-& Gt ShutDown (' CREATE TABLE failed!        ');        } $this->currinfos = Array ();        $this->allowfiletype = Array (); $this->setmessage (' initialization complete!    '); The Public Function Setallowfiletype ($fileext) {if (!in_array ($fileext, $this->allowfileType)) $this->allowfiletype[] = $fileext; The 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:%.6f seconds! Scan to File%dA ', (Microtime (True)-$this->timestart), $this->filecount)); foreach ($this->outmessage as $k = + $v) {echo '. $v. '
'; } exit (); Public Function Resetall () {$this->tabletruncate (); $this->sethandle (); $this->setmessage (' already 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 into". $this->tablename. " SET {$setsql} "); }} Public Function Dirisbefore ($path 1, $path 2) {$path 1 = strtolower ($path 1); $path 2 = Strtolower ($path 2); if ($path 1 = = $path 2) return TRUE; $arr 1 = explode ('/', $path 1); $arr 2 = explode ('/', $path 2); $arr 1Len = count ($arr 1); $arr 2Len = count ($arr 2); $i = 0; while (true) {if ($i > $arr 1Len && $i > $arr 2Len) break; if ($arr 1[$i]! = $arr 2[$i]) {return $this->comparebefore ($arr 1[$i], $arr 2[$i]); } $i + +; } return FALSE; }//Before and after text compare public function Comparebefore ($str 1, $str 2) {if ($str 1 = = $str 2) return TRUE; $str 1Length = strlen ($str 1); $str 2Length = strlen ($str 2); $i = 0; while (true) {if ($i > $str 1Length && $i > $str 2Length) break; $k 1 = $str 1{$i}; $k 2 = $str 2{$i}; if ($k 1! = $k 2) {return (ord ($k 1) < Ord ($k 2))? True:false; } $i + +; } return FALSE; The Public function scan ($path = ") {//default starts from the root directory to scan if (empty ($path)) $path = $this->webroot; $this->sethandle ($path); 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)) {$a 1 = pathinfo ($filepath ); Filter type if (! $this->isallowfiletype ($a 1[' extension ')) {return; } $arr [' is_file '] = 1; $arr [' is_dir '] = 0; $arr [' fileext '] = $a 1[' extension '); $arr [' filename '] = $a 1[' filename ']. $a 1[' 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 '; }//Product detail Page 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 ']); Prevent duplicate Write Data } return $arr; Public Function Fileext ($filepath) {return substr ($filepath, STRRCHR ($filepath, '. ')); The Public Function Record ($filepath) {$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); The Public Function Tablehas () {return $this->db->getone (' SHOW TABLES like \ '. $this->tablename. ' \ ") = = $this->tablename? True:false; } public Function Summary () {$size 1M = 1024*1024; $sqlarr = Array (' Total number of files: ' = ' = ' 1 ', ' greater than 1 m: ' = ' + ' filesize > '. $size 1M, ' size 0: ' = ' filesize=0 ', ' Dangerous Documents' = = ' Fileext REGEXP ' Exe|dll|zip|rar ' ",); $message = $comma = "; foreach ($sqlarr as $k = = $v) {$r = $this->db->getone ("Select COUNT (*) from". $this->tablena Me. " 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, * $key)-1 )//1024B will be displayed as 1KB {$num _bitunit_str = (ceil ($num/pow (2, $key) * 100)/100). "$bitunit [$key]"; }} return $num _bitunit_str; Public Function Real_url ($filepath) {return str_replace ($this->webroot, ", $filepath); } public Function __destruct () {Clearstatcache (); }}




Using 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 complete! Click here to view the results! ');


defects:No implementation starts scanning the entire station from the specified directory. There is a directory comparison, function Dirisbefore (), but if the current start directory is "/zzzzzz.zip", and then there is a folder "/aaaaa/aaaaaa/ddddd/", still will scan to the ~ contrast is not completely implemented.
The main people to help me see this is not in line with the OOP specification? What are the shortcomings of the wording


Reply to discussion (solution)

Your class can't run on its own, that's the biggest problem.

This doesn't feel like a class, it's like changing the process-oriented abruptly to object-oriented (singleton)

  • Related Article

    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.