简体   繁体   中英

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")
{
  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, eg:

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM