dir.php
<html>
<!-- dir.php script von 25.11.18
show files in directories with php (and without apache2 option indexes, which we are not allowed to set in .htaccess in hoststar
version with direct links to jpg etc. which we do want to pass directly to the browser and not to highlight_file
-->
<head>
<title>file browser</title>
</head>
<body>
<?php
if (empty($_GET)) { $_GET['default'] = '.'; }
foreach ($_GET as $key => $value) { // use $value in $key . are replace by _, this is bad for filenames!
$fuPa = realpath($value); // absolute path
$rePa = substr($fuPa, strlen(getcwd())); // relative path
$shPa = substr($rePa, substr($rePa, 0 , 1) === '/'); // don't show leading slash if present
// echo '<p> fu=' . $fuPa . ' re=' . $rePa . ' shPa=' . $shPa;
if ($fuPa === FALSE) {
echo '<h3>bad or nonexisting file ' . $value . '</h3>';
} else if (strPos($fuPa, getcwd()) !== 0) { // allow only subdirectories of my own directory
echo '<h3>outside directory ' . $value . '</h3>';
} else if (is_dir($fuPa)) {
echo '<h3> directory ' . $fuPa . '</h3> <ol>';
foreach (scandir($fuPa) as $fi) { // show hyperlinks to files in directory
$ff = substr($rePa . '/' . $fi, 1);
$ext = strtolower(pathinfo($fi)['extension'] ?? '');
$ee = $fi . /* ' ext=' . $ext . ' mime=' . mime_content_type($ff) . ' ff=' . $ff . */ '</a></li>' ;
if (is_file($ff) && strpos(".php.css.java.", '.' . $ext . '.') === FALSE) {
echo '<li><a href="' . $ff . '">' . $ee;
} else {
echo '<li><a href="'. $_SERVER['SCRIPT_NAME'] . '?f=' . $ff . '" '
. (is_dir($ff) ? 'style="background-color: #a0ffa0;"' : '') . '>' . $ee ;
}
}
echo '</ol>';
} else if (is_file($fuPa)) {
echo '<h3>' . $shPa .'</h3>';
highlight_file($fuPa);
} else {
echo '<h3>' . $shPa . ' filetype=' . filetype($fuPa) . ' not supported</h3>';
}
}
function mimeType($ff) {
if (false) {
echo '<h1> mime-content-type ' . mime_content_type($ff) . " for $ff</h1>";
// jExit();
return mime_content_type($ff); // does not work for abc/my.xls !
} else {
static $mimeTypes = array(
'pdf' => 'application/pdf',
// 'txt' => 'text/plain',
'html' => 'text/html',
'exe' => 'application/octet-stream',
'zip' => 'application/zip',
'doc' => 'application/msword',
'xls' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
'gif' => 'image/gif',
'png' => 'image/png',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpg',
// 'php' => 'text/plain'
);
return isset($mimeTypes[$ty = strtolower(pathinfo($ff, PATHINFO_EXTENSION))]) ? $mimeTypes[$ty] : null;
}
}
?>
</body>
</html>