Skip to content

Instantly share code, notes, and snippets.

@kamawanu
Last active February 9, 2022 00:22
Show Gist options
  • Save kamawanu/e1c370b26dcff54e76a4155f014f95e0 to your computer and use it in GitHub Desktop.
Save kamawanu/e1c370b26dcff54e76a4155f014f95e0 to your computer and use it in GitHub Desktop.
tiny directory browser
<?php
$DIR = explode("/", $_SERVER["PATH_INFO"]);
##if( $_SERVER["SCRIPT_NAME"] != $_SERVER["REQUEST_URI"] ){
## header("Refresh: 0;URL=${_SERVER["SCRIPT_NAME"]}");
## exit;
##}
###var_dump($_SERVER);
if (count($DIR) == 1) {
header("Refresh: 0;URL=./index.php/");
exit;
}
array_shift($DIR);
$root = array();
foreach (glob("root.*") as $r1) {
list($__root, $post) = explode(".", $r1);
$root[$post] = $r1;
}
##var_dump($root);
#
###var_dump($root);
if ($DIR[0] == "") {
foreach ($root as $post => $r1) {
echo "<A HREF=\"./$r1/\" >$post</a> ";
}
exit();
}
$PATHS = array();
while (count($DIR) > 0 && $DIR[0][0] != ">" && $DIR[0] != "=") {
$dir0 = glob("*");
foreach ($dir0 as $ii => $fn) {
if (!is_dir($fn)) {
unset($dir0[$fn]);
}
}
$PATHS[$DIR[0]] = array_values($dir0);
chdir(array_shift($DIR));
}
##var_dump($DIR);
##$PATHS[] = glob("*");
if ($DIR[0] == "=") {
##print_r(getcwd());
@array_shift($DIR);
##print_r($DIR);
$maybetype = array_pop(explode(".", $DIR[0]));
##var_dump($DIR[1]);
header("Content-type: image/$maybetype");
fpassthru(fopen($DIR[0], "r"));
exit;
}
$depth = array_keys($PATHS);
$offset = $DIR[0][0] == ">" ? 1 : 0;
while (count($depth) > 0) {
$lk = array_shift($depth);
unset($fs);
$fs = & $PATHS[$lk];
## print_r($fs);
$iixdepth = str_repeat("../", count($depth) + $offset);
if (count($fs) == 1) {
echo "&nbsp; $lk";
} else {
$ii = array_search($lk, $fs);
if ($ii !== false) {
if ($ii > 0) {
$iipp = $ii - 1;
echo "<A HREF=\"${iixdepth}/${fs[$iipp]}/\">${fs[$iipp]}</A> ";
}
echo "&lt;$ii &gt; <font color=red>$lk</font> ";
$nnii = $ii + 1;
if ($nnii < count($fs)) {
echo " <a href=\"${iixdepth}${fs[$nnii]}/\">${fs[$nnii]}</a>";
}
} else {
echo " <font color=green>$lk</font> ";
}
}
echo "<BR>";
unset($fs);
# $iidepth .= "../";
}
if ($DIR[0][0] == ">") {
### var_dump($PATHS);
##print_r(getcwd());
##var_dump($DIR);
$fn0 = substr($DIR[0], 1);
$IS_IMAGE = preg_match("/\.(png|jpe?g)$/", $fn0);
$FILES = glob("*");
sort($FILES);
if ($IS_IMAGE) {
for ($ii = 0; $ii < count($FILES); ++$ii) {
if (!preg_match("/\.(png|jpe?g)$/", $FILES[$ii])) {
unset($FILES[$ii]);
}
}
$FILES = array_values($FILES);
}
$count = count($FILES);
$pos = array_search($fn0, $FILES);
echo "<A HREF=\"../.\">UP</A> $pos/$count <BR>";
## var_dump($pos);
$prev = false;
if ($pos > 0) {
$prev = $FILES[$pos - 1];
echo "<A HREF=\"&gt;$prev\">$prev</A><BR>";
}
$next = false;
$next_num = $pos + 1;
if ($next_num < $count) {
$next = $FILES[$next_num];
}
echo "<IMG HEIGHT=\"90%\" SRC=\"./=/$fn0\"/><BR>";
if ($next) {
echo "NEXT => $next_num <A HREF=\"&gt;$next\">$next</A><BR>";
}
unset($FILES);
echo "<HR>";
## exit();
} else {
##print_r(getcwd());
echo "<BR>";
echo "<A HREF=\"../.\">UP</A> $pos/$count ";
foreach (glob("*") as $fn) {
if (is_file($fn)) {
echo "<A HREF=\"./&gt;$fn\">$fn</A> <BR>";
} elseif (is_dir($fn)) {
echo "<A HREF=\"./$fn/\">$fn</A> <BR>";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment