PHP - Directory viewer
So I'm working on a simple administrator page for my web server. I'm trying to create a simple file manager that lists directories/files and lets you change directory / edit files. I'm having a couple problems though. My first problem is that it just shows files and folders but wont distinguish between them. Like I want folders to have a / in front of them so the admin knows it's a folder not a file. Also, I'm having a problem when trying to change directories. If I change to the any directory it wont work. Here is my current code:
<?php
echo '
<form name="read" method="POST">
Directory: <input type="text" name="read" />
<input type="submit" value="Go" />
</form>';
$maindir = "/home/amartin/public_html";
$no = "No access";
$dir = $_POST['read'];
if($dir == "/")
{
echo $no;
die();
}
elseif($dir == "/home")开发者_C百科
{
echo $no;
die();
}
elseif($dir == "/home/")
{
echo $no;
die();
}
elseif($dir == "/home/amartin")
{
echo $no;
die();
}
elseif($dir == "/home/amartin/")
{
echo $no;
die();
}
else {
$dir = $maindir;
}
echo "Viewing directory: " . $dir;
$folders = scandir($dir);
chdir($dir);
foreach($folders as $ind_file)
{
echo $ind_file.'<br/>';
}
?>
You can use the is_dir function to check if the path points to a directory.
Also, you could use regex to make the checks easier, e.g.:
if (preg_match('~/home(/amartin)?/?~', $dir))
etc.
You can check your current working directory with getcwd() and change it with chdir. Are you using relative paths when changing the directories?
精彩评论