Anteprima

Versione del 23 lug 2013 alle 09:24 di WikiSysop (discussione | contributi) (Creata pagina con '<pre> <?php // TODO: verificare se il percorso di partenza esiste ////////////////////////////////////////////////////////////////////////////////////// MAIN $NL = "\r\n"...')
(diff) ← Versione meno recente | Versione attuale (diff) | Versione più recente → (diff)
<?php


// TODO: verificare se il percorso di partenza esiste


////////////////////////////////////////////////////////////////////////////////////// MAIN

$NL = "\r\n";
//Put here the directory you want to search for. Put / if you want to search your entire domain
$dir = './';

//Put the date you want to compare with in the format of:  YYYY-mm-dd hh:mm:ss
$comparedatestr = "2013-04-25 00:00:00";
$comparedate = strtotime($comparedatestr);

//I run the function here to start the search.
$exts = array();
$exts[] = "log";
$array = directory_tree(null, $dir, $comparedate, $exts);
echo "array size is " . count($array) . $NL;

////////////////////////////////////////////////////////////////////////////////////// FUNCTIONS


//This is the function which is doing the search...
function directory_tree($array=array(), $address, $comparedate, $exts=array(), $newline="\r\n", $dir_sep="/"){

  @$dir = opendir($address);
  if($dir){
        while($entry = readdir($dir)){
                $address = formatDir($address);
                $fullpath = $address . $dir_sep . $entry;
                if(is_dir($fullpath) && ($entry != ".." && $entry != ".")){
                        $array = directory_tree($array, $fullpath, $comparedate, $exts, $newline, $dir_sep);
                } else {

                  if($entry != ".." && $entry != ".") {
                    $last_modified = filemtime($fullpath);
                    $last_modified_str= date("Y-m-d h:i:s", $last_modified);
                    $ok = true;
                    if(count($exts)>0){
                                $ext = pathinfo($fullpath, PATHINFO_EXTENSION);
                                // echo "ext: " . $ext;
                                $ok = in_array($ext, $exts);
                    }
                    if($ok){
                                if($comparedate < $last_modified)  {
                                  echo $fullpath . ' => ' . $last_modified_str . $newline;
                                  $array[$fullpath] = $last_modified;
                                }
                    }
                 }
            }
      }
}
return $array;
}




function formatDir($string){

//if(substr($string, -1) == '/') {
//    $string = substr($string, 0, -1)
//}

//Another (probably better) option would be using rtrim() - this one removes all trailing slashes:

$string = rtrim($string, '/');

  return $string;
}

function mostRecentModifiedFileTime($dirName, $doRecursive) {
    $d = dir($dirName);
    $lastModified = 0;
    while($entry = $d->read()) {
        if ($entry != "." && $entry != "..") {
            if (!is_dir($dirName."/".$entry)) {
                $currentModified = filemtime($dirName."/".$entry);
            } else if ($doRecursive && is_dir($dirName."/".$entry)) {
                $currentModified = mostRecentModifiedFileTime($dirName."/".$entry,true);
            }
            if ($currentModified > $lastModified){
                $lastModified = $currentModified;
            }
        }
    }
    $d->close();
    return $lastModified;
}

?>


<?php

// TODO: verificare se il percorso di partenza esiste

////////////////////////////////////////////////////////////////////////////////////// MAIN

$NL = "\r\n"; //Put here the directory you want to search for. Put / if you want to search your entire domain $dir='./';

//I run the function here to start the search. $exts = array(); // $exts[] = "log"; $array = directory_tree(null, $dir, $exts); echo "array size is " . count($array) . $NL;

////////////////////////////////////////////////////////////////////////////////////// FUNCTIONS


//This is the function which is doing the search... function directory_tree($array=array(), $address, $exts=array(), $newline="\r\n", $dir_sep="/"){

 @$dir = opendir($address);
 if($dir){
       while($entry = readdir($dir)){
               $address = formatDir($address);
               $fullpath = $address . $dir_sep . $entry;
               if(is_dir($fullpath) && ($entry != ".." && $entry != ".")){
                       $array = directory_tree($array, $fullpath, $exts, $newline, $dir_sep);
               } else {
                 if($entry != ".." && $entry != ".") {
                   $ok = true;
                   if(count($exts)>0){
                               $ext = pathinfo($fullpath, PATHINFO_EXTENSION);
                               // echo "ext: " . $ext;
                               $ok = in_array($ext, $exts);
                   }
                   if($ok){

$sha1 = get_sha1_file($fullpath); $md5 = get_md5_file($fullpath);

                                 echo $fullpath . ' => ' . "SHA1: " . $sha1 . " MD5: " . $md5 . $newline;
                                 $array[$fullpath] = array($sha1, $md5);
                   }
                }
           }
     }

} return $array; }


function formatDir($string){

//if(substr($string, -1) == '/') { // $string = substr($string, 0, -1) //}

//Another (probably better) option would be using rtrim() - this one removes all trailing slashes:

$string = rtrim($string, '/');

 return $string;

}

function get_sha1_file($file) {

  if(is_null($file) || !file_exists($file)) {

return trigger_error('File is null or does not exists');

  }
  return sha1_file($file);

}

function get_md5_file($file) {

  if(is_null($file) || !file_exists($file)) {

return trigger_error('File is null or does not exists');

  }
  return md5_file($file);

}


?>