here is my stab at it...
a full webpage... just copy and paste and you are ready to go
parts are efficient.. there is a binary sort algorithm, and i make use of hashed as they are used in lower level languages... however most the speed this gives is taken away by my use of array_splice in the placement portion... oh well...
[edit]
this forum insists on line-wrapping my ]; on line 155.. this will throw an error if copied and pasted... beware
[/edit]
punchline:
this works on my computer.. and should give you what you want
<?php
$base_dir = isset($_GET['base_dir']) ? $_GET['base_dir'] : './';
$recurse = isset($_GET['recurse']);
if ( !preg_match('/[\\/\\\]$/',$base_dir) ) {
$base_dir .= '/';
}
$recurse_checked = $recurse ? ' checked="checked"' : '';
print <<<END_FORM
<form action="{$_SERVER['PHP_SELF']}" method="get">
<table align="center">
<tr>
<th bgcolor="#CCCCCC" colspan="2">Choose Directory</th>
</tr><tr>
<td>Base Dir</td><td><input type="text" name="base_dir" value="{$base_dir}" /></td>
<tr></tr>
<td>Recurse</td><td><input name="recurse" type="checkbox"{$recurse_checked} /></td>
<tr></tr>
<td><input type="submit" /></td><td> </td>
</tr>
</table>
</form>
<br /><br />
END_FORM;
if ( ($files = scoutDir( $base_dir, $recurse )) == false ) {
echo "That Directory Does Not Exist";
} else {
$compare_key = 'bytes';
$sorted_by_size = sort_by_key( $compare_key, $files );
$meg_plus = over_a_meg($files);
echo "
<table width=\"100%\">
<tr>
<th bgcolor=\"#CCCCCC\" colspan=\"4\">Larger Than A Meg ({$base_dir})</th>
</tr>
<tr bgcolor=\"#CCCCCC\">
<th>Name</th>
<th>Path</th>
<th>Bytes</th>
<th>Megs</th>
</tr>
";
$color = true;
foreach ( $meg_plus as $index ) {
$color = !$color;
$bgcolor = $color ? "#EEEEEE" : "#FFFFFF";
echo "
<tr bgcolor=\"{$bgcolor}\">
<td>{$files[$index]['name']}</td>
<td>{$files[$index]['path']}</td>
<td>{$files[$index]['bytes']} bytes</td>
<td>{$files[$index]['megs']} megs</td>
</tr>\n";
}
echo "
</table><br /><br />
";
echo "
<table width=\"100%\">
<tr>
<th bgcolor=\"#CCCCCC\" colspan=\"4\">All Files ({$base_dir})</th>
</tr>
<tr bgcolor=\"#CCCCCC\">
<th>Name</th>
<th>Path</th>
<th>Bytes</th>
<th>Megs</th>
</tr>
";
$color = true;
foreach ( $sorted_by_size as $index ) {
$color = !$color;
$bgcolor = $color ? "#EEEEEE" : "#FFFFFF";
echo "
<tr bgcolor=\"{$bgcolor}\">
<td>{$files[$index]['name']}</td>
<td>{$files[$index]['path']}</td>
<td>{$files[$index]['bytes']} bytes</td>
<td>{$files[$index]['megs']} megs</td>
</tr>\n";
}
echo "
</table><br /><br />
";
}
### Functions below
function scoutDir( $dir_path, $recurse=false )
{
$sub_dirs = array();
$files = array();
if ($dir = @opendir($dir_path)) {
while ( ($file = readdir($dir)) !== false ) {
if ( $file == '.' || $file == '..' ) {
continue;
} else if ( is_dir($dir_path.$file) ) {
$sub_dirs[] = $dir_path.$file;
} else {
$stats = stat($dir_path.$file);
$files[] = array( 'name'=>$file,
'path'=>$dir_path.$file,
'bytes'=>$stats[7],
'megs'=>sprintf( '%01.6f', $stats[7]/1048576 ) );
}
}
closedir($dir);
if ( $recurse ) {
foreach ( $sub_dirs as $sub_dir ) {
$files = array_merge( $files, scoutDir( $sub_dir.'/', $files, $recurse ) );
}
}
return $files;
}
return false;
}
function over_a_meg( &$files )
{
$meg_plus = array();
foreach ( $files as $index=>$file ) {
if ( $file['megs'] >= 1 ) {
$meg_plus[] = $index;
}
}
return $meg_plus;
}
function sort_by_key( $compare_key, &$raw_data )
{
$sorted_results = array();
foreach ( $raw_data as $index=>$item ) {
binary_insert( $index, $compare_key, $raw_data, $sorted_results );
}
return $sorted_results;
}
function binary_insert( $insert_index, $compare_key, &$raw_data, &$sorted_indexes )
{
$low = 0;
$high = count($sorted_indexes) - 1;
$last_mid = 0;
while ($low <= $high) {
$this_mid = floor(($low + $high) / 2);
$compare_this = $raw_data[$insert_index][$compare_key];
$compare_to = $raw_data[$sorted_indexes[$this_mid]][$compare_key];
if ( is_numeric($compare_this) && is_numeric($compare_to) ) {
if ( $compare_this > $compare_to ) {
$this_comparison = 1;
} else if ( $compare_this < $compare_to ) {
$this_comparison = -1;
} else {
$this_comparison = 0;
}
} else {
$this_comparison = strcmp($compare_this,$compare_to);
}
$insert_offset = $this_comparison>0 ? 1 : 0;
if ($this_comparison == 0 || $high==$low) {
array_insert( $insert_index, $this_mid+$insert_offset, $sorted_indexes );
return;
} else {
if ($this_comparison < 0) {
$high = $this_mid - 1;
} else {
$low = $this_mid + 1;
}
}
$last_mid = $this_mid;
}
array_insert( $insert_index, max($last_mid,$this_mid), $sorted_indexes );
}
function array_insert( $item, $position, &$array )
{
$first_part = array_slice($array, 0, $position );
array_push( $first_part, $item );
$second_part = array_slice($array, $position );
$array = array_merge($first_part,$second_part);
}
?>