php/googleClientLib/dir.php
<html>
<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_file($fuPa)) {
echo '<h3>' . $shPa .'</h3>';
highlight_file($fuPa);
} 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);
echo '<li><a href="http://' . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'] . '?f=' . $ff . '" '
. (is_dir($ff) ? 'style="background-color: #a0ffa0;"' : '') . '>' . $fi . '</a>' ;
}
echo '</ol>';
} else {
echo '<h3>' . $shPa . ' filetype=' . filetype($fuPa) . ' not supported</h3>';
}
}
?>
</body>
</html>