62 lines
1.6 KiB
PHP
Raw Normal View History

2024-11-05 13:16:49 +02:00
<?php
// config
$ext_descriptions = [
// 'doc' => 'Word document',
];
// utils
function human_filesize($size,$unit="") {
if( (!$unit && $size >= 1<<30) || $unit == "GB")
return number_format($size/(1<<30),2)."GB";
if( (!$unit && $size >= 1<<20) || $unit == "MB")
return number_format($size/(1<<20),2)."MB";
if( (!$unit && $size >= 1<<10) || $unit == "KB")
return number_format($size/(1<<10),2)."KB";
return number_format($size)." bytes";
}
// parse files
$files_sorted = [];
$dir = new DirectoryIterator(__DIR__ . '/files');
foreach ($dir as $fileinfo) {
if ($fileinfo->isDot()) continue;
$ext = pathinfo(__DIR__ . '/files/' . $fileinfo->getFilename(), PATHINFO_EXTENSION);
$filesize = filesize(__DIR__ . '/files/' . $fileinfo->getFilename());
$filesize_human = human_filesize(round($filesize));
$basename = basename($fileinfo->getFilename());
if (!isset($files_sorted[$ext])) $files_sorted[$ext] = [];
$files_sorted[$ext][] = [
'filename' => $fileinfo->getFilename(),
'basename' => $basename,
'path' => 'files/' . $fileinfo->getFilename(),
'extension' => $ext,
'size' => $filesize,
'size_human' => $filesize_human,
];
}
// sort files by size
function sort_files($a, $b) {
return $a['size'] > $b['size'];
}
// sort file extensions
ksort($files_sorted);
// output
foreach ($files_sorted as $ext => $files) {
usort($files, 'sort_files');
?>
<h3><?php echo $ext_descriptions[$ext] ?? $ext ?></h3>
<ul>
<?php foreach ($files as $file) { ?>
<li>
<a href="<?php echo $file['path']; ?>" download><?php echo $file['basename']; ?></a>
</li>
<?php } ?>
</ul>
<?php }