Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
autoindex php
<?php
declare(strict_types=1);
autoindex();
function autoindex()
{
$tohtml = function (string $str): string {
return htmlentities($str, ENT_QUOTES | ENT_HTML401 | ENT_SUBSTITUTE | ENT_DISALLOWED, 'UTF-8', true);
};
$files = [];
$dir = new DirectoryIterator(dirname(__FILE__));
foreach ($dir as $file) {
$files[] = ($file->getFilename());
}
natsort($files);
foreach ($files as $file) {
$file_html = $tohtml($file);
if ($file_html === '.') {
$file_url = $file_html . '?' . $tohtml(http_build_query($_GET));
}else{
$file_url = $file_html;
}
echo "<a target='_blank' href='{$file_url}'>{$file_html}</a><br/>\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment