note 119351 deleted from features.commandline.webserver by peehaa

From: Date: Wed, 19 Oct 2016 09:32:03 +0000
Subject: note 119351 deleted from features.commandline.webserver by peehaa
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-206920@lists.php.net to get a copy of this message
Note Submitter: gabe at fijiwebdesign dot com ---- 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)); ?>

« previous php.notes (#206920) next »