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(-) (limited to 'index.php') 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 From 840fcad8b08b266b1c350d46db8e22ea07367902 Mon Sep 17 00:00:00 2001 From: Phil Burton Date: Tue, 2 Apr 2019 11:01:48 +0100 Subject: Add default sort back --- index.php | 3 +++ 1 file changed, 3 insertions(+) (limited to 'index.php') diff --git a/index.php b/index.php index dbc9dea..fec487b 100644 --- a/index.php +++ b/index.php @@ -95,6 +95,9 @@ $nameurl = "?C=N;O=A"; $modifiedurl = "?C=M;O=A"; $sizeurl = "?C=S;O=A"; +// Set default sort +$sort = ['S', 'A']; + if (isset($_GET['C'])) { $sort = preg_split("/;O=/", $_GET['C']); if ($_GET['C'] == "N;O=A") { -- cgit v1.2.3 From 919a300bd993feb9ecb518f226c4d8d0882b7ff6 Mon Sep 17 00:00:00 2001 From: Phil Burton Date: Tue, 2 Apr 2019 11:03:20 +0100 Subject: Switch to use date as default sort --- index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'index.php') diff --git a/index.php b/index.php index fec487b..754c5ed 100644 --- a/index.php +++ b/index.php @@ -96,7 +96,7 @@ $modifiedurl = "?C=M;O=A"; $sizeurl = "?C=S;O=A"; // Set default sort -$sort = ['S', 'A']; +$sort = ['M', 'A']; if (isset($_GET['C'])) { $sort = preg_split("/;O=/", $_GET['C']); -- cgit v1.2.3 From 856c953060c89b8c76b637d0e6d2fe11f3ca8b58 Mon Sep 17 00:00:00 2001 From: Phil Burton Date: Tue, 2 Apr 2019 11:04:07 +0100 Subject: Sort default by descending --- index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'index.php') diff --git a/index.php b/index.php index 754c5ed..8de2218 100644 --- a/index.php +++ b/index.php @@ -96,7 +96,7 @@ $modifiedurl = "?C=M;O=A"; $sizeurl = "?C=S;O=A"; // Set default sort -$sort = ['M', 'A']; +$sort = ['M', 'D']; if (isset($_GET['C'])) { $sort = preg_split("/;O=/", $_GET['C']); -- cgit v1.2.3