1 回答

TA贡献2065条经验 获得超14个赞
当您单击一个链接时,它会在您的浏览器中导航,除非您在服务器上设置了 URL 重写,否则将加载该 URL 上的任何内容。您需要使用 GET(查询)参数将您选择的目录传递给您的 PHP 文件,并在显示锚点时使用它。
我对下面的代码有一些自由,但我相信它可以满足您的需求。脚本的名称是从从任何查询参数中剥离的当前 URL 中读取的,并且dir查询参数用于将文件夹名称传递给锚点中的脚本。将这些结合起来,URL 看起来像这样,具体取决于您如何运行它:
/folder/script.php?dir=foldername%2Ffolder2
请注意,该值是 URL 编码的,这通常是一种很好的做法,每当您将任意字符串作为查询参数传递时。在下面的代码中,文件名输出也包含在htmlspecialchars()其中转义浏览器可能识别为标记并尝试解析的任何 HTML 字符,从而导致页面分崩离析。
为了增加安全性,我调用了在运行时ini_set将open_basedir设置更改为当前目录的调用,以避免目录遍历攻击,例如?dir=../../../../etc/passwd可用于访问敏感系统信息的攻击。虽然不是您最初的要求,但我相信这也应该在您的情况下处理,我强烈建议保留此安全措施。
此外,还对点文件(.和..)进行了一些处理,以确保未列出当前目录,并且您无法从基本目录向上导航。
我还使用HEREDOC使代码比多次调用print. 随之而来的唯一小不便是您不能在字符串中放入太复杂的表达式,因此您必须将它们移动到上面的变量中,但我相信这也有助于提高可读性。
<?php
// Credit to http://jeffreysambells.com/2012/10/25/human-readable-filesize-php
function human_file_size($bytes, $decimals = 2) {
$size = array('B', 'KB', 'MB', 'GB');
$factor = (int)floor((strlen($bytes) - 1) / 3);
return round($bytes / pow(1024, $factor), 2).' '.@$size[$factor];
}
function dirlist() {
// Prevent malicious users from reading files in directories above
ini_set('open_basedir', __DIR__);
$baseDirectory = '.'.DIRECTORY_SEPARATOR;
// Get directory from query parameter
$directoryPath = $baseDirectory.(!empty($_GET['dir']) ? rtrim($_GET['dir'], '\\/').DIRECTORY_SEPARATOR : '');
$myDirectory = opendir($directoryPath);
$isTopLevel = $directoryPath === $baseDirectory;
while ($entry = readdir($myDirectory)){
if ($entry === '.' || ($isTopLevel && $entry === '..')){
continue;
}
$dirListArray[] = $entry;
}
$fileCount = count($dirListArray);
sort($dirListArray);
print <<<HTML
<p class="heading">$fileCount FILES / FOLDER FOUND</p>
<table width="100%" border="1" cellpadding="5" cellspacing="0" class="whitelinks">
<tr>
<th>FILE/FOLDER NAME</th>
<th>FILE TYPE</th>
<th>FILE SIZE</th>
</tr>
HTML;
// Get current URL without query parameters
// Trim everything after and including "?"
$scriptPath = strtok($_SERVER['REQUEST_URI'], '?');
foreach ($dirListArray as $indexValue){
$htmlEncodedIndex = htmlspecialchars($indexValue);
$fileType = filetype($directoryPath.$indexValue);
$fileSize = human_file_size(filesize($directoryPath.$indexValue));
if ($fileType === 'dir'){
if ($indexValue === '..'){
// Link to top level, no rectory separator in string
if (strpos($indexValue, DIRECTORY_SEPARATOR) === false)
$queryParam = '';
// Link to subdirectory
else {
$parts = explode(DIRECTORY_SEPARATOR, $indexValue);
array_pop($parts);
// Assemble query param (make sure to URL encode!)
$queryParam = '?dir='.urlencode(implode(DIRECTORY_SEPARATOR, $parts));
}
}
// Assemble query param (make sure to URL encode!)
else $queryParam = '?dir='.urlencode($indexValue);
$href = $scriptPath.$queryParam;
}
else $href = $directoryPath.$indexValue;
print <<<HTML
<tr>
<td>
<a href='$href'>$htmlEncodedIndex</a>
</td>
<td>$fileType</td>
<td>$fileSize</td>
</tr>
HTML;
}
print '</table>';
}
?>
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
.heading {
color: #CCC;
padding: 0;
margin: 5px;
}
#directory-list-container {
margin: 7px;
padding: 0;
border: 3px solid #000;
outline: 1px solid #666;
}
#directory-list-content {
margin: 0;
padding: 5px;
border: 2px solid #666;
}
#directory-list-content a,
#directory-list-container a:link {
color: #00f;
text-decoration: none !important;
}
#directory-list-container a:hover {
color: #0F0;
}
#directory-list-container a:active {
color: #090;
}
#directory-list-container a:visited {
color: #DDD;
}
</style>
</head>
<body>
<div id="directory-list-container">
<div id="directory-list-content">
<?php dirlist(); ?>
</div>
</div>
</body>
</html>
- 1 回答
- 0 关注
- 190 浏览
添加回答
举报