简体   繁体   English

PHP-目录查看器

[英]PHP - Directory viewer

So I'm working on a simple administrator page for my web server. 因此,我正在为我的Web服务器创建一个简单的管理员页面。 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. 您可以使用is_dir函数来检查路径是否指向目录。

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. 您可以使用getcwd()检查当前的工作目录,并使用chdir对其进行更改。 Are you using relative paths when changing the directories? 更改目录时是否使用相对路径?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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