<?PHP
function uplevel($dir) {
$dir = explode('/', $dir);
$ndir = "/";
for($i =1; ; $i++) {
if($i==(count($dir)-1)) { break; }
$ndir .= $dir[$i].'/';
}
//just returns a link to the root and to go up a directory ".."
return '<a href="?dir=/">Root</a> - <a href="?dir='.$ndir.'">^Dir</a>';
}
function dirlisting($dir) {
//prints out a list of the direcotyr
if(substr($dir, strlen($dir)-1, 1) != "/") { $dir .= '/'; }
$files = scandir($dir);
$echo = "<pre>Files is this Directory:\n";
foreach($files as $file) {
//prints with links =)
$echo .= "\t<a href=\"?dir=".$dir.$file."\">$file</a>\n";
}
return $echo."</pre>";
}
function readcontents($file) {
//if a file is selected then print its contents in a pretty textbox
if (is_readable($file)) {
$file = explode("\n", file_get_contents($file));
} else {
$file[] = "Permission denied";
}
//technically not read-only but save that for another tut
$echo = "<pre><center>--Read-Only--</center>\n<textarea style='width:100%;height:700px;'>";
foreach($file as $line) {
//broke it up by line because just printing it out wouldnt be formatted
$echo .= $line."\n";
}
return $echo."</textarea></pre>";
}
if($_GET['dir']=="") {
//if directory isnt set go to default:
$dir = '/';
} else {
//else go here =)
//notice: no filtering!
$dir = $_GET['dir'];
}
//start $echo out
echo uplevel($dir);
if(is_dir($dir)) {
//if its a directory go to this function
echo dirlisting($dir);
} elseif(file_exists($dir)) {
//files? no problem!
echo readcontents($dir);
}
?>
<?PHP
function listallfiles($dir, $ext) {
//check if we have permissions first
if(is_readable($dir)) {
//scandir in php puts all items into an array
$files = scandir($dir);
//loop through files
foreach($files as $file) {
//if its a directory use recursion
if(is_dir($dir.$file)) {
//make sure its not .. or .
if(substr($file, -1, 1) != ".") {
//recursive functions rule!
$folders .= listallfiles($dir.$file."/", $ext);
}
} else {
//if not a directory see if the last part of the file
//has $ext
$start = strlen($ext) - (strlen($ext)*2);
$end = strlen($ext);
if(substr($file, $start, $end) == $ext) {
//if so were gonna add it to the variable
$files .= $dir.$file."\n";
}
}
}
//lastly we print it all out
return $folders.$files;
}
}
$file = $_GET['q'];
$ext = $_GET['e'];
if(!is_dir($file)) {
//default directory
$file = "/";
}
if(strlen($ext)==0) {
//default extension
//may want to upgrade to enable more than one
//example: .php .phtml
$ext = ".php";
}
echo "\n<br /><br />";
echo '<form method="get"><input style="width:350px;" type="text" name="q" value="'.$file.'"><input style="width:50px;" type="text" name="e" value="'.$ext.'"><input type="submit" value="Search!"></form>';
echo "<pre>All {$ext} files is {$file}:\n";
//starts the function
echo listallfiles($file, $ext);
Disclaimer: All code is given as is. I accept no responsibility how you implement it any way or form. Please express caution when using any code given to you. Use at your own risk.


Sign In
Create Account



Back to top









