Jump to content

Directory Details Code


fhumayun

Recommended Posts

I've been trying consolidate some code recipes for getting PHP to recursively walk a (windows UNC or *Nix) directory path and collect all the file details into an array (which I will later flatten and perform parsing, and file operations like moving and ftp'ing)

The problem I'm having here so far:
[blockquote]1) I can't seem to figure out why the looping halts abruptly, therefore only producing one result.
2) The problem aforementioned may relate, but I don't understand, why my time-difference determination is not working correctly.[/blockquote]
I'm really hoping to have some expert eyes point to any mistakes I'm not seeing.
Many thanks,

[code]<?php
// Include some vanity related functions
require_once 'function.size_readable.php';
// Set maximum execution time for recursion results
set_time_limit(900);


//-------------------------------------------------------------------------------------------------
//          Function to recursively scan and collect details about files it finds in folders
//-------------------------------------------------------------------------------------------------     
function rec_scandir($dir)
{  
   //$files = array();  // declaring arrays
   //$files_processed = array();
   if ( $handle =opendir($dir) ) {   // check if folder is valid
       while ( ($file =readdir($handle)) !=false ) {  // loop while walking directory
           if ( $file !=".." && $file !=".") {   // skip folders ..  and .
               if ( is_dir($dir . "/" . $file) ) {
                   $files_processed[$file] =rec_scandir($dir . "\\" . $file); // list directory
               }else {  // Listing file details
                        // Changed $file into an array with more information about the files
                         $files = array("name" => $file,
                                       "path" => $dir . "\\" .$file,
                                       //"size" => filesize($dir . "/" . $file),
                                       "friendly_size" => size_readable(filesize($dir . "/" . $file), 'MB', '%01.2f %s'),  
                                       "type" => filetype($dir . "/" . $file),
                                       "last_change" => $last_change = date("F d Y h:i:s", filemtime($dir . "/" . $file)),                                    
                                       );
                                        
                        //
                        //          Perform File Age check
                        //                    
                            $contents = $last_change; // contents of date collected in array
                            $delimiter = " ";  // define the delimiter
                            $splitcontents = explode($delimiter, $contents);  // php parsing function
                            $regDate=strtotime($contents); //grab date contents
                                    
                            //caluculate our time from GMT time
                            $hour=gmdate('H');
                            $min=gmdate('i');
                            $sec=gmdate('s');
                            $month=gmdate('n');
                            $day=gmdate('d');
                            $year=gmdate('Y');
                            $stamp=mktime ($hour,$min,$sec,$month,$day,$year);
                            $server_time= date("F d Y h:i:s", mktime($hour,$min,$sec,$month,$day,$year));
                                                        
                            $datediff = $stamp - $regDate;  // difference calculated in seconds
                            
                            // Time measurments
                            if($datediff < 3600 )  
// if time is less than an hour (60 minutes) report minutes
                            {
                                $age = $on = floor($datediff/60). ' minute(s)';  
                            }
                            else if($datediff < 86400 )
// if time is less than an day (24 hours) then report hours
                            {
                                $age = $on =  floor($datediff/60/60).' hour(s)';
                            }
                            else if($datediff < 604800 )
// if time is less than an week (7days)report days
                            {
                                $age = $on =  floor($datediff/60/60/24). ' day(s)';
                            }
                            else if($datediff < 2419200 )
// if time is less than a month (4 weeks) report weeks
                            {
                                $age = $on =  floor($datediff/60/60/24/7). ' week(s)';
                            }
                            else if($datediff < 29030400)
// if time is less than a year (12 months) report months
                            {
                                $age = $on =  floor($datediff/60/60/24/7/4). ' month(s)';
                            }
                            else // go ahead and report year(s)
                            {
                                $age = $on =  floor($datediff/365/60/60/24). ' year(s)';
                            }                
                                            
                        
                        //this second array that has the age of file calculated from last_change variable in previous array
                        $process_age = array("age" => $age,
                                             "server_time" => $server_time);
                        
                        // merging results of second array with first
                        $files_processed = array_merge($files,
                                             $process_age
                                            );
               }
           }          
       }
       closedir($handle);  // closing out folder
       return $files_processed;
   }
}

function cmp($a, $b)
{
   if ( is_string($a) && is_string($b) ) {
       return strcmp($a, $b) > 0 ? 1 : -1;
   }elseif ( is_int($a) && is_int($b) ) {
       return $a > $b ? 1 : -1;
   }elseif ( is_int($a) && is_string($b) ) {
       return 1;
   }elseif ( is_string($a) && is_int($b) ) {
       return -1;
   }else {
       return 0;
   }
}

function array_ukmultisort(&$arr, $func) {
   uksort($arr, $func);
   while ( list($key, $val) =each($arr) ) {
       if ( is_array($val) ) {
           array_ukmultisort($arr[$key], $func);
       }
   }
}

function get_size($path)  {
       if(!is_dir($path))return filesize($path);
       $dir = opendir($path);
       while($file = readdir($dir))
       {
           if(is_file($path."/".$file))$size+=filesize($path."/".$file);
           if(is_dir($path."/".$file) && $file!="." && $file !="..")$size +=get_size($path."/".$file);
          
       }
       return $size;
}

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;
}

// specify directory to monitor
//$pdir = "\\\\tapedrive\\c\\To_Be_Processed";

// perform recursive display details on parent folder
$dir =rec_scandir($pdir);
$flatten_array =natural_list($dir);

// collect details into a multidimensional array
array_ukmultisort($dir, "cmp");

/* selective display function
function week_old($dir_basket) {
    return ($dir_basket['last_change'] <
*/

// aim to browser
echo "<pre>";
echo $pdir . " Folder Size: ".size_readable(get_size($pdir), 'MB', '%01.1f %s')."\n";
//print_r($flatten_array);
print_r($dir);
echo "</pre>";
?>[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.