note 55253 deleted from ref.filesystem by felipe

From: Date: Mon, 10 Dec 2007 17:59:59 +0000
Subject: note 55253 deleted from ref.filesystem by felipe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-133232@lists.php.net to get a copy of this message
Note Submitter: Ciprian Danea, cipriandanea at yahoo dot com ---- Here is a little script that will enumerate a directory (given as a string) recursively. The main working function is get_dir($path,$max_depth='') Optional aguments: $max_depth : obvious it'll limit the recursive depth. Not so optional: $l=0 : the first call must omit this, or explicitly set it to 0, since it represents the starting depth $total='': the current total filesize; it should also be left alone, although it autosets itself to 0 on the first call; In order to do something with the files other than just echoing them, simply replace the echo functions with your own; Thanks to: dave at birko dot cjb dot net (for the nice filesize function) vbwebprofi at gmx dot de (for the fileperms function) function get_dir($path,$max_depth='',$l=0,$total=''){ if(!is_dir($path))return; $path=substr($path,-1)!="/"?$path."/":$path; if(!$l){ echo "\nEnumerating directory $path :\n\n"; $total=0; } if($max_depth==='' || ($max_depth>$l && is_int($max_depth))) $test_depth=true; else $test_depth=false; $pre=""; $c=$l; while($c--)$pre.="\t"; $dir=opendir($path); while($f=readdir($dir)){ if($f=="."||$f=="..")continue; $file=$path.$f; $size=""; if(is_file($file)||!is_dir($file)){ $s=filesize($file); $total+=$s; $size="[ ".fsize($s)." ]"; } else $f.="/"; while(strlen($size)<16) $size=" ".$size; echo "\n".get_permissions(fileperms($file)).$size.$pre."\t".$f; if(is_dir($file) && $test_depth) $total=get_dir($file,$max_depth,$l+1,$total); } if(!$l) echo "\n\nTotal size: ".fsize($total); return $total; } function get_permissions($fperms) { $out; if($fperms & 0x1000) // FIFO pipe $out = 'p'; elseif($fperms & 0x2000) // Character outecial $out = 'c'; elseif($fperms & 0x3000) // Socket [ original value 0xD000, wrong for linux, but this is also registering as a directory... ant ideas?] $out = 's'; elseif($fperms & 0x4000) // Directory $out = 'd'; elseif($fperms & 0x6000) // Block outecial $out = 'b'; elseif($fperms & 0x8000) // Regular $out = '-'; elseif($fperms & 0xA000) // Symbolic Link $out = 'l'; else // UNKNOWN $out = 'u'; // owner $out .= (($fperms & 0x0100) ? 'r' : '-') . (($fperms & 0x0080) ? 'w' : '-') . (($fperms & 0x0040) ? (($fperms & 0x0800) ? 's' : 'x' ) : (($fperms & 0x0800) ? 'S' : '-')); // group $out .= (($fperms & 0x0020) ? 'r' : '-') . (($fperms & 0x0010) ? 'w' : '-') . (($fperms & 0x0008) ? (($fperms & 0x0400) ? 's' : 'x' ) : (($fperms & 0x0400) ? 'S' : '-')); // world $out .= (($fperms & 0x0004) ? 'r' : '-') . (($fperms & 0x0002) ? 'w' : '-') . (($fperms & 0x0001) ? (($fperms & 0x0200) ? 't' : 'x' ) : (($fperms & 0x0200) ? 'T' : '-')); return $out; } function fsize($size) { $a = array("B", "KB", "MB", "GB", "TB", "PB"); $pos = 0; while ($size >= 1024) { $size /= 1024; $pos++; } return round($size,2)." ".$a[$pos]; } //usage example get_dir("/tmp/"); // full depth get_dir("/tmp/",4); //maximum depth set to 4

« previous php.notes (#133232) next »