<?php // Get the filename of the current page $PHP_SELF = $_SERVER['PHP_SELF']; function recursiveReadDir($path){ global $PHP_SELF, $basePath, $baseUrl; // Open the directory for reading $dir = @opendir("$basePath/$path"); if($dir){ echo "<b>[<a href='$PHP_SELF?path=$path'>DIR </a>] " . basename($path) . "</b>\n"; echo "<ul>\n"; // Loop through each directory while ($file = readdir($dir)) { if($file == '.' || $file == '..')continue; elseif(is_dir("$basePath/$path/$file")){ // Current file is a directory, so read content of the new directory echo "<li>\n"; recursiveReadDir("$path/$file"); echo "</li>"; }else{ echo "<li>[FILE] <a href=\"$baseUrl$path/$file\">$file</a></li>\n"; } } closedir($dir); echo "</ul>\n"; }else{ echo "Unable to open directory. Click <a href='$curFile'>here</a> to go back."; } } ?> <html> <head> <title>Directory Browser</title> </head> <body> <?php // The base path for browsing $basePath = realpath('/var/www/vhosts/mydomain.com/httpdocs/files'); // Url corresponding to base path $baseUrl = 'http://www.mydomain.com/files'; // Get the location $path = empty($_GET['path']) ? '.' : $_GET['path']; // Find the absolute path to the location $path = realPath("$basePath/$path"); // If the location is below the base url, exit // This prevents users from browsing parent directories using .. if(eregi('^' . preg_quote($basePath), $path)){ // Get the actual relative path from the basepath $curPath = substr($path, strlen($basePath) + 1); $arPath = explode('/', str_replace('\\', '/', $curPath)); $path = ''; // Bread Crumb for navigation echo "<div><a href='$PHP_SELF'>Home</a>"; if(empty($arPath[0]))unset($arPath[0]); foreach($arPath as $p){ $path .= "/$p"; echo " > <a href='$PHP_SELF?path=$path'>$p</a>"; } echo '</div>'; // Recursively list the directory recursiveReadDir($curPath); }else{ echo "You are not allowed to access this file directly. Click <a href='$PHP_SELF'>here</a> to go back."; } ?> </body> </html> |
Tutorials, Chrome extensions Code samples JavaScript Angular CSS HTML. Updated regularly. Visit and subscribe to the rss feed.
Tuesday, December 30, 2008
Php - Recursive Directory Listing
Recursive listing of all the files / directories in directory. Includes security measure to prevent users from browsing the parent directory.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment