Jump to content

sorting list of directories by count of contents


hellonoko

Recommended Posts

I am trying to display a list of directories an the count of their file contents sorted by volume.

 

Have been playing around with a few things but I can't quite get it to work.

 

<?php

$dir = "/home2/sharingi/public_html/subdomains";

$dh = opendir($dir);

while (false !== ($filename = readdir($dh)))
{
	if ($filename !== "." && $filename !== "..")
	{
		//$files[] = $filename;

		//$files[] = count(glob("/home2/sharingi/public_html/subdomains/" . $filename . "/filez/" . "*.mp3"));

		$files['user'][$x] = $filename;
		$files['count'][$x] = $files[] = count(glob("/home2/sharingi/public_html/subdomains/" . $filename . "/filez/" . "*.mp3"));

		$x++;

	}
}

sort ($files);

//for $i = 0; $i < sizeof($files); $i++;
//{
//	echo $files['user'][$value];
//	echo '<br>';
//	echo $files['count'][$value];
//	echo '<br>';

//	i++;
//}

foreach ( $files as $value)
{
	echo $files['user'][$value];
	echo '<br>';
	echo $files['count'][$value];
	echo '<br>';
}

//unset $value;

//print_r($files);

?>

 

Any suggestions?

Link to comment
Share on other sites

$dir = "/home2/sharingi/public_html/subdomains";
$out = shell_exec("du " . $dir . " -k --max-depth=2");
foreach(preg_split("#\n#", $out) as $val)
     $thedirs[] = preg_split('#\s#', $val);

function sortSize( $a, $b ){
    if ( $a[0] == $b[0] )
        return 0;
    if ( $a[0]          return -1;
    return 1;
}
usort($thedirs, "sortSize");
foreach ($thedirs as $val)
     echo $val[1] . "          (" . $val[0] . ")
\n";
?>

Link to comment
Share on other sites

no idea how this works but returns:

 

()

/home2/sharingi/public_html/subdomains/raphael/cgi-bin (4)

/home2/sharingi/public_html/subdomains/raphael/tmp (4)

/home2/sharingi/public_html/subdomains/iana/cgi-bin (4)

/home2/sharingi/public_html/subdomains/test/tmp (4)

/home2/sharingi/public_html/subdomains/iana/tmp (4)

/home2/sharingi/public_html/subdomains/test/cgi-bin (4)

/home2/sharingi/public_html/subdomains/iana/filez (4264)

/home2/sharingi/public_html/subdomains/iana (4304)

/home2/sharingi/public_html/subdomains/raphael/filez (14692)

/home2/sharingi/public_html/subdomains/raphael (14732)

/home2/sharingi/public_html/subdomains/test/filez (131628)

/home2/sharingi/public_html/subdomains/test (131668)

/home2/sharingi/public_html/subdomains (150708)

 

 

was hoping for something more like.

 

test 13

raphael 2

iana 1

Link to comment
Share on other sites

Added some comments

$dir = "/home2/sharingi/public_html/subdomains";
//This is a Linux command to get your directories
/*
--max-depth controls the amount of directories it goes deep
-k means the size is returned in kilobytes
if you want megabytes instead do -m or -b for just bytes
*/
$out = shell_exec("du " . $dir . " -m --max-depth=1");

//This split everyone one of them each into separate lines to work with..since they're returned as a whole
foreach(preg_split("#\n#", $out) as $val)
     $thedirs[] = preg_split('#\s#', $val); //The separates the size from the dirname and puts the array

//This is a function to sort the dir's size in ascending order....
function sortSize( $a, $b ){
    if ( $a[0] == $b[0] )
        return 0;
    if ( $a[0]          return -1;
    return 1;
}
usort($thedirs, "sortSize");
foreach ($thedirs as $val)
     echo $val[1] . "          (" . $val[0] . ")
\n";
?>

Link to comment
Share on other sites

I think I see how this works but isn't it still going to not work for me since I need a count of files not the size.

 

If use A had 10 files all totaling 1meg and user B had 2 files each 10megs then I still need to sort user A first.

 

Here is my newest code that reads all the info into an array. I think it works correct but I am not sure how to get it to display the info.

 

<?php

$dir = "/home2/sharingi/public_html/subdomains";

$dh = opendir($dir);



while (false !== ($filename = readdir($dh)))
{
	if ($filename !== "." && $filename !== "..")
	{

		$files['name'] = $filename;

		$files['count'] = count(glob("/home2/sharingi/public_html/subdomains/" . $filename . "/filez/" . "*.mp3"));


		echo $files['name'];
		echo '<br>';
		echo $files['count'];
		echo '<br>';

	}
}

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.