note 119351 added to features.commandline.webserver
| From: | gabe at fijiwebdesign dot com | Date: | Thu, 19 May 2016 07:22:46 +0000 |
| Subject: | note 119351 added to features.commandline.webserver | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-205512@lists.php.net to get a copy of this message | ||
Here's a router.php that will show directory listings and allow traversal of the directories as
well as load index.html and index.php from the directory if they exist.
<?php
// router for php built in server to show directory listings.
// php -S localhost:8001 router.php
$path = $_SERVER['DOCUMENT_ROOT'] . $_SERVER["REQUEST_URI"];
$uri = $_SERVER["REQUEST_URI"];
// let server handle files or 404s
if (!file_exists($path) || is_file($path)) {
return false;
}
// append / to directories
if (is_dir($path) && $uri[strlen($uri) -1] != '/') {
header('Location: ' . $uri . '/');
}
// send index.html and index.php
$indexes = ['index.php', 'index.html'];
foreach($indexes as $index) {
$file = $path . '/' . $index;
if (is_file($file)) {
return require($file);
}
}
// show directory list
echo "<h2>Index of " . $uri . "</h2>";
$g = array_map(function($path) {
if (is_dir($path)) {
$path .= '/';
}
return str_replace('//', '/', $path);
}, glob($path . '/*'));
usort($g, function($a,$b) {
if(is_dir($a) == is_dir($b))
return strnatcasecmp($a,$b);
else
return is_dir($a) ? -1 : 1;
});
echo implode("<br>", array_map(function($a) {
$url = str_replace($_SERVER['DOCUMENT_ROOT'], '', $a);
return '<a href="' . $url . '">' . substr($url, 1) .
'</a>';
}, $g));
?>
----
Server IP: 72.52.91.14
Probable Submitter: 187.188.9.163
----
Manual Page -- http://php.net/manual/en/features.commandline.webserver.php
Edit -- https://master.php.net/note/edit/119351
Del: integrated -- https://master.php.net/note/delete/119351/integrated
Del: useless -- https://master.php.net/note/delete/119351/useless
Del: bad code -- https://master.php.net/note/delete/119351/bad+code
Del: spam -- https://master.php.net/note/delete/119351/spam
Del: non-english -- https://master.php.net/note/delete/119351/non-english
Del: in docs -- https://master.php.net/note/delete/119351/in+docs
Del: other reasons-- https://master.php.net/note/delete/119351
Reject -- https://master.php.net/note/reject/119351
Search -- https://master.php.net/manage/user-notes.php