hellonoko Posted October 10, 2008 Share Posted October 10, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/127903-sorting-list-of-directories-by-count-of-contents/ Share on other sites More sharing options...
fanfavorite Posted October 10, 2008 Share Posted October 10, 2008 First, make sure you define $x at the top: $x = 1; then change your foreach ($files as $value) to: for ($count = 1;$count <= $x; $count++) { echo $files['user'][$count]; echo '<br>'; echo $files['count'][$count]; echo '<br>'; } Quote Link to comment https://forums.phpfreaks.com/topic/127903-sorting-list-of-directories-by-count-of-contents/#findComment-662296 Share on other sites More sharing options...
hellonoko Posted October 10, 2008 Author Share Posted October 10, 2008 Returns 0 should return three numbers Quote Link to comment https://forums.phpfreaks.com/topic/127903-sorting-list-of-directories-by-count-of-contents/#findComment-662366 Share on other sites More sharing options...
Zane Posted October 11, 2008 Share Posted October 11, 2008 $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"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/127903-sorting-list-of-directories-by-count-of-contents/#findComment-662551 Share on other sites More sharing options...
hellonoko Posted October 11, 2008 Author Share Posted October 11, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/127903-sorting-list-of-directories-by-count-of-contents/#findComment-662574 Share on other sites More sharing options...
Zane Posted October 11, 2008 Share Posted October 11, 2008 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"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/127903-sorting-list-of-directories-by-count-of-contents/#findComment-662820 Share on other sites More sharing options...
hellonoko Posted October 11, 2008 Author Share Posted October 11, 2008 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>'; } } Quote Link to comment https://forums.phpfreaks.com/topic/127903-sorting-list-of-directories-by-count-of-contents/#findComment-662844 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.