I use this code to print the subfolders name and size in a HTML table, what I want to do is save this table as a 2d array so it can be sorted by folder size (since right now is sorted by alphabetical name order)
<?php
$directory = "F:/directory";
echo "<table>";
$depth = 0;
$count = 0;
$ritit = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST);
$r = array();
foreach ( $ritit as $splFileInfo ) {
$count +=1;
if ($ritit->getDepth() === $depth && $splFileInfo->isDir()) {
echo "<tr><td>".stripslashes($splFileInfo)."</td>";
echo "<td>".getSize($splFileInfo)."</td></tr>";
}
}
echo "</table>";
function getSize($dir, $precision = 2) {
$ritit = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS));
$bytes = 0;
foreach ( $ritit as $v ) {
$bytes += $v->getSize();
}
$bytes = max($bytes, 0);
return round($bytes, $precision) . ' ';
}
?>
This is the HTML with the folder name on the left and the folder size on the right
Store the filename as the key and the filesize as the value in $r 2D-array. Use the sort function to the sort the array using value.
Here is the link to sort the array: https://www.w3schools.com/php/php_arrays_sort.asp
<?php
$directory = "F:/directory";
echo "<table>";
$depth = 0;
$count = 0;
$ritit = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST);
$r = array();
foreach ( $ritit as $splFileInfo ) {
$count +=1;
if ($ritit->getDepth() === $depth && $splFileInfo->isDir()) {
// echo "<tr><td>".stripslashes($splFileInfo)."</td>";
// echo "<td>".getSize($splFileInfo)."</td></tr>";
$r[stripslashes($splFileInfo)] = getSize($splFileInfo);
}
}
// sort the associative array using the value.
asort($r);
foreach($r as $key => $value) {
echo "<tr><td>".$key."</td>";
echo "<td>".$value."</td></tr>";
}
echo "</table>";
function getSize($dir, $precision = 2) {
$ritit = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS));
$bytes = 0;
foreach ( $ritit as $v ) {
$bytes += $v->getSize();
}
$bytes = max($bytes, 0);
return round($bytes, $precision) . ' ';
}
?>