PHP Classes

Proposed updates

Recommend this page to a friend!

      PHP File Change Scanner  >  All threads  >  Proposed updates  >  (Un) Subscribe thread alerts  
Subject:Proposed updates
Summary:Some changes you might consider
Messages:1
Author:Richard de Breyn
Date:2015-07-30 10:54:33
 

  1. Proposed updates   Reply   Report abuse  
Picture of Richard de Breyn Richard de Breyn - 2015-07-30 10:54:34
Hi,

I like the class. Exactly what I need for my release management system. I've modified the non-mysql sections to suit my needs. The mySQL section I've removed from mine as I have my own wrappers.

For the mySQL environment, I'd suggest moving to PDO. Doing something like this (pseudocode):
1. With PHP 5.3+, open connection with
$p = new PDO($dsn, $u, $p, array(PDO::MYSQL_ATTR_FOUND_ROWS => true));. The reason for this is that if you run an update to mysql and the record hasn't changed, the rowCount() will return 0. You don't want this, you want it to tell you 1 if the record exists and 0 if the record does not exist.
2. Do an update of the record (file). Check how many records are effected with rowCount().
3. If Step 2 returns 1, then you know the record exists and it will have been updated already, so there is no need to do any insert. If it returns 0, then you must insert the record.

This will make your code smaller and will lower your overhead (I believe) from first checking if the record exists and then inserting it or updating it. With this method, you'll execute a minimum of 1 SQL query and a maximum of 2, instead of a constant 2 with your current method.

I've also remove most other methods and incorporated them all into a single method getListFiles().

The modified code (with DB section removed, sorry) is:
<?php
/*
File scanner by Dmitry Stroganov 2015
Modified by daBrain afterwards.
*/

class fileScanner {
public $path = array();
public $exceptions = array();
public $tz = "GMT";
public $all_files = array();
public $failed = array();

function __construct($paths=NULL, $exceptions=NULL){
date_default_timezone_set($this->tz);
if(!is_null($paths)) $this->path = $paths;
if(!is_null($exceptions)) $this->path = $exceptions;
}

function scan() {
foreach ($this->path as $p) {
$this->getListFiles($p);
}
}

function getListFiles($dir){
if(is_readable($dir)){
$fp = opendir($dir);
while($cv_file = readdir($fp)) {
$fFullPath = $dir."/".$cv_file;
if(!in_array($fFullPath, $this->exceptions)){
if(is_file($fFullPath)) {
if(is_readable($fFullPath)){
$this->all_files[] = array(
"NAME" => $fFullPath,
"HASH" => md5(file_get_contents($fFullPath)),
"TIME" => filemtime($fFullPath),
"SIZE" => filesize($fFullPath),
);
}else{
$this->failed[] = $fFullPath;
}
}elseif($cv_file!="." && $cv_file!=".." && is_dir($fFullPath)){
$this->getListFiles($fFullPath);
}
}
}
closedir($fp);
}else{
$this->failed[] = $dir;
}
}
}

$scanner = new fileScanner();
$scanner->path = array("/home/genie");
$scanner->exceptions = array(
"/home/genie/domains" ,
"/home/genie/docs" ,
"/home/genie/vendor" ,
"/home/genie/xx__OTHER" ,
"/home/genie/xcache" ,
"/home/genie/logs" ,
"/home/genie/new" ,
"/home/genie/backupsystem"
);
$scanner->scan();