PHP - 目录查看器
所以我正在为我的网络服务器开发一个简单的管理员页面。我正在尝试创建一个简单的文件管理器,它列出目录/文件并允许您更改目录/编辑文件。不过我有几个问题。我的第一个问题是它只显示文件和文件夹,但不会区分它们。就像我希望文件夹前面有一个 / 这样管理员就知道这是一个文件夹而不是文件。另外,我在尝试更改目录时遇到问题。如果我更改为任何目录,它将无法工作。这是我当前的代码:
<?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/>';
}
?>
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/>';
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 is_dir 函数检查路径是否指向目录。
另外,您可以使用正则表达式使检查更容易,例如:
等。
您可以使用 getcwd() 并用 chdir 更改它。更改目录时是否使用相对路径?
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.:
etc.
You can check your current working directory with getcwd() and change it with chdir. Are you using relative paths when changing the directories?