diff options
-rw-r--r-- | index.php | 79 |
1 files changed, 54 insertions, 25 deletions
@@ -95,6 +95,9 @@ $nameurl = "?C=N;O=A"; $modifiedurl = "?C=M;O=A"; $sizeurl = "?C=S;O=A"; +// Set default sort +$sort = ['M', 'D']; + if (isset($_GET['C'])) { $sort = preg_split("/;O=/", $_GET['C']); if ($_GET['C'] == "N;O=A") { @@ -113,7 +116,7 @@ date_default_timezone_set("GB"); $files_directory = "./"; -$new_array = array(); +$fileCollection = array(); $files = array(); $fdirectory = opendir("$files_directory"); @@ -124,56 +127,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 +224,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 +327,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++; } |