From b57bb1e8bb5fabcba4a2b584f08310c4479d5e6f Mon Sep 17 00:00:00 2001 From: Fbenas Date: Tue, 2 Apr 2019 00:23:49 +0100 Subject: Stop using modified date as a key in array by using an assoc array. Here we have add sizes as an item, and move the date key in too. We then ditch new array, sizes array and ordered array for a single collection that we mainpulate with usort. The listing array is then built off this --- index.php | 76 ++++++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 51 insertions(+), 25 deletions(-) diff --git a/index.php b/index.php index c229199..dbc9dea 100644 --- a/index.php +++ b/index.php @@ -113,7 +113,7 @@ date_default_timezone_set("GB"); $files_directory = "./"; -$new_array = array(); +$fileCollection = array(); $files = array(); $fdirectory = opendir("$files_directory"); @@ -124,56 +124,82 @@ while ($file = readdir($fdirectory)) { } } -foreach ($files as $key=> $file) { +foreach ($files as $key => $file) { $c_date = filemtime("$files_directory/$file"); - $new_array[$c_date] = $file; - $size_array[$c_date] = filesize("$files_directory/$file"); + $fileCollection[] = [ + 'date' => $c_date, + 'file' => $file, + 'size' => filesize("$files_directory/$file") + ]; } if (isset($sort)) { // Sort by modified date if ($sort[0] == "M") { if ($sort[1] == "A") { - ksort($new_array); + usort( + $fileCollection, + function($a, $b) { + return $a['date'] > $b['date']; + } + ); } else { - krsort($new_array); + usort( + $fileCollection, + function($a, $b) { + return $a['date'] < $b['date']; + } + ); } - $ordered_array = $new_array; // Sort by name } else if ($sort[0] == "N") { if ($sort[1] == "A") { - asort($new_array); + usort( + $fileCollection, + function($a, $b) { + return $a['file'] > $b['file']; + } + ); } else { - arsort($new_array); + usort( + $fileCollection, + function($a, $b) { + return $a['file'] < $b['file']; + } + ); } - $ordered_array = $new_array; // Sort by size } else if ($sort[0] == "S") { if ($sort[1] == "A") { - asort($size_array); + usort( + $fileCollection, + function($a, $b) { + return $a['size'] > $b['size']; + } + ); } else { - arsort($size_array); + usort( + $fileCollection, + function($a, $b) { + return $a['size'] < $b['size']; + } + ); } - $ordered_array = $size_array; } } else { - krsort($new_array); - $ordered_array = $new_array; + krsort($fileCollection); } $listsize = 0; -while ($this_array = each($ordered_array)) { - $key = $this_array['key']; - $value = $new_array[$key]; - +foreach ($fileCollection as $item) { // Only show last X entries if requested if (isset($_GET['last']) && ($listsize > $_GET['last'] - 1)) { break; } // Drop if on index ignore list - if (in_array($value, $indexignore)) { + if (in_array($item['file'], $indexignore)) { continue; } @@ -195,12 +221,12 @@ while ($this_array = each($ordered_array)) { $to = time(); } - if ($key < $from || $key > $to) { + if ($item['date'] < $from || $item['date'] > $to) { continue; } } - $fileparts = explode(".", strtolower($value)); + $fileparts = explode(".", strtolower($item['file'])); $icon = "unknown"; switch ($fileparts[count($fileparts)-1]) { @@ -298,10 +324,10 @@ while ($this_array = each($ordered_array)) { break; } - $listing[$listsize]['filename'] = utf8_encode($value); + $listing[$listsize]['filename'] = utf8_encode($item['file']); $listing[$listsize]['type'] = $icon; - $listing[$listsize]['modified'] = $key; - $listing[$listsize]['size'] = $size_array[$key]; + $listing[$listsize]['modified'] = $item['date']; + $listing[$listsize]['size'] = $item['size']; $listsize++; } -- cgit v1.2.3