php - 如何使用 php 链接和浏览子目录
问题描述
我想使用 PHP 制作一个简单的文件浏览器。因为我是 PHP 新手。我需要一些推动,
<table class="table">
<thead>
<tr>
<th scope="col">Directory Name</th>
<th scope="col">Last Modified</th>
<th scope="col">Size</th>
</tr>
</thead>
<tbody>
<?php
$folders= new DirectoryIterator(__DIR__);
while($folders->valid()){
?>
<tr>
<td><?php echo "<a href='{$folders->current()}'>{$folders->current()}</a>" ?></td>
<td></td>
<td><?php echo $folders->getSize();?></td>
</tr>
<?php
$folders->next();
} ?>
</tbody>
</table>
到目前为止,我已经做到了这一点。如何完成该代码才能使用 php.ini 文件浏览器功能。谢谢你。
解决方案
严格的安全基本路径、带返回的文件夹导航、带文件内容视图的文件导航。DIV 布局,没有 JavaScript 和简单的 PHP。
<?php
$Script = basename(__FILE__);
$RootPath = "F:\Web";
$FileHTML = [];
$Path = realpath("" . (isset($_GET["Path"]) ? $_GET["Path"] : $RootPath) . "" . (isset($_GET["Folder"]) ? "/{$_GET["Folder"]}" : null));
if(substr($Path, 0, strlen($RootPath)) !== $RootPath)$Path = $RootPath;
foreach(scandir($Path) as $Item)if($Item != "." && ($Path != $RootPath || $Item != "..")){
$ItemWithPath = "{$Path}/{$Item}";
if(is_dir($ItemWithPath)){
$FolderHTML[] = "<div class=\"Item\"><a href=\"./{$Script}?Path={$Path}&Folder={$Item}\" class=\"Link\">{$Item}</a></div>";
}
else{
//$FileHTML[] = "<div class=\"Item\"><a href=\"./{$Script}?Path={$Path}&Folder=.&{$Item}&File={$Item}\" class=\"Link\">{$Item}</a><span class=\"Size\">" . round(filesize($ItemWithPath) / 1024, 0) . " KB</span><span class=\"ModificationTime\">" . date("Y-m-d H:i:s", filemtime($ItemWithPath)) . " KB</span></div>";
$FileHTML[] = "<tr class=\"Item\"><td><a href=\"./{$Script}?Path={$Path}&Folder=.&{$Item}&File={$Item}\" class=\"Link\">{$Item}</a></td><td class=\"Size\">" . round(filesize($ItemWithPath) / 1024, 0) . " KB</td><td class=\"ModificationTime\">" . date("Y-m-d H:i:s", filemtime($ItemWithPath)) . "</td></tr>";
}
}
?><html>
<head>
<style>
body{margin: 0; font-family: Verdana, Tahoma, Arial; font-size: 12px;}
body:after{display: block; clear: both; content: '';}
div{box-sizing: border-box;}
.Navigation{float: left; width: 33%;}
.Navigation > .CurrentPath{border: 1px Black solid; background: rgba(0, 255, 255, 0.25); padding: 5px;}
.Navigation > .FolderList{height: calc(50% - 1em - 12px); border: 1px Grey solid; overflow-y: auto;}
.Navigation > .FolderList > .Item{padding: 5px;}
.Navigation > .FolderList > .Item:hover{background: rgba(255, 255, 0, 0.5);}
.Navigation > .FolderList > .Item > .Link{text-decoration: none;}
.Navigation > .FileList{height: 50%; border: 1px Grey solid; overflow-y: auto;}
.Navigation > .FileList > table{font-size: inherit;}
.Navigation > .FileList > table > tbody > .Item{}
.Navigation > .FileList > table > tbody > .Item:hover{background: rgba(255, 255, 0, 0.5);}
.Navigation > .FileList > table > tbody > .Item > td{padding: 5px;}
.Navigation > .FileList > table > tbody > .Item > td > .Link{text-decoration: none;}
.Navigation > .FileList > table > tbody > .Item > .Size{margin-left: 2em; white-space: nowrap;}
.Navigation > .FileList > table > tbody > .Item > .ModificationTime{margin-left: 2em;}
.FileContent{float: left; width: 67%;}
.FileContent > .FileName{border: 1px Black solid; background: rgba(255, 0, 255, 0.25); padding: 5px;}
.FileContent > .Content{height: calc(100% - 1em - 12px); background: Black; padding: 5px; color: White; white-space: pre;}
</style>
</head>
<body>
<div class="Navigation">
<div class="CurrentPath"><?=$Path?></div>
<div class="FolderList">
<?=implode("\n ", $FolderHTML) . "\n"?>
</div>
<div class="FileList">
<table>
<tbody>
<?=implode("\n ", $FileHTML) . "\n"?>
</tbody>
</table>
</div>
</div>
<div class="FileContent">
<div class="FileName"><?=isset($_GET["File"]) ? $_GET["File"] : null?></div>
<div class="Content"><?=isset($_GET["File"]) ? file_get_contents("{$Path}/{$_GET["File"]}") : null?></div>
</div>
</body>
</html>
推荐阅读
- java - Kotlin 中的 public static void main
- java - 如何将数组链接到方法
- c# - C# - 比较 UNC 路径
- soap - 将 OnVif PullPoint 服务与 Zeep 一起使用
- python - 在 Raspberry Pi 3 上使用 PyOpenGL 获取 glReadPixels(错误:1282,描述:b'invalid operation')
- elixir - 允许 Ecto 查询在测试环境中立即运行?
- javascript - LocalStorage addClass 多值
- java - JDateChooser 无法解析为类型
- node.js - 如何使用 fs.readdir 获取标准文件夹列表
- java - $PATH 中没有 java