您好,欢迎访问一九零五行业门户网

【请点评】写的一个OOP面向对象的类,功能:扫描整站文件夹!

功能:扫描整站所有指定类型的文件,并且收集其路径存储到数据库。(用于筛选出大小为0的文件)
完成:我用的oop来写的,不过总是感觉很多不足。以前基本上不用oop,全都过程。求点评此“类”。
备注:其实里面有一个功能没有实现,就是支持从指定从指定目录开始扫描整站。
代码:
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->shutdown('创建表失败!'); } $this->currinfos = array(); $this->allowfiletype = array(); $this->setmessage('初始化完毕! '); } 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('总计耗时:%.6f 秒!扫描到文件%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('已经重设所有!'); } 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($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; } //文字前后对比 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) webroot; $this->sethandle($path); if(file_exists($path)) { if(is_dir($path)) { $this->setmessage('正扫描:'.$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); //过滤类型 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); //商品列表页 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']); //防止重复写入数据 } return $arr; } public function fileext($filepath) { return substr($filepath,strrchr($filepath,'.')); } public function record($filepath) { $infos = $this->getfileinfos($filepath); if(!empty($infos)) $this->addrow($infos); return true; } public function tablecreate() { $sql =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( '总共文件数目:' => ' 1 ', '大于1m:' => ' filesize > '.$size1m, '大小为0:' => ' filesize=0 ', '危险的文件' => 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 = pow(2, 10 * $key) - 1) // 1024b 会显示为 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(); }}
使用代码:
if(!is_object($fscan)) $fscan = new filescan(); $fscan->resetall(); $fscan->setallowfiletype('htm'); $fscan->setallowfiletype('html'); $fscan->scan(root_path.'product'); $fscan->summary(); $fscan->shutdown('扫描完毕!点击这里查看结果!');
缺陷:没有实现从指定目录开始扫描整站。里面加了目录对比的,function dirisbefore(),但是如果当前开始目录为 “/zzzzzz.zip”,然后存在文件夹“/aaaaa/aaaaaa/ddddd/”的时候,仍旧是会扫描到~ 对比没有彻底实现。
主要大家帮我看看这样是不是符合oop规范?写法有哪些不足
回复讨论(解决方案) 你的类不能独立运行,这是最大的毛病
这个感觉不像个类,倒像把面向过程硬生生改成面向对象(单例)
其它类似信息

推荐信息