'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'); ?>