我正在构建一个目录浏览器,当我找到信息时,我发现this question,令我惊讶的是代码比我预期的要容易得多,我能够理解它,所以我正在使用它在我的项目上(我对php很新,我从不使用我不理解的代码)。它运作良好,我做了一些美学上的改变。现在问题来了,由于某种原因,我无法更改根目录,我有这个:
<?php
session_start();
if(!isset($_SESSION['username'])){
header("Location: ./test.php");
}
?>
<!doctype html>
<html>
<head>
<title>CoroCloud</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="./js/material.min.js"></script>
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="https://code.getmdl.io/1.3.0/material.pink-indigo.min.css" />
</head>
<html>
<body>
<div class="mdl-layout mdl-js-layout mdl-layout--fixed-header">
<header class="mdl-layout__header">
<div class="mdl-layout__header-row">
<!-- Title -->
<span class="mdl-layout-title">Cloud</span>
<!-- Add spacer, to align navigation to the right -->
<div class="mdl-layout-spacer"></div>
<!-- Navigation. We hide it in small screens. -->
<nav class="mdl-navigation mdl-layout--large-screen-only">
<?php
echo "<a class=\"mdl-navigation__link\" href=\"\">{$_SESSION['username']}</a>";
?>
</nav>
</div>
</header>
<div class="mdl-layout__drawer">
<span class="mdl-layout-title">CATEGORIES</span>
<nav class="mdl-navigation">
<a class="mdl-navigation__link" href="">File</a>
<a class="mdl-navigation__link" href="">Images</a>
<a class="mdl-navigation__link" href="">Music</a>
<a class="mdl-navigation__link" href="">Films</a>
</nav>
</div>
<main class="mdl-layout__content" style="background-color: white; background-image: url('https://i.warosu.org/data/tg/img/0357/97/1414469732022.gif'); background-size: auto 100%; background-repeat: no-repeat; background-position: center;">
<center>
<div class="page-content" style="padding: 24px; flex: none; align-items: center; justify-content: center;">
<?php
// Snippet from PHP Share: http://www.phpshare.org
function formatSizeUnits($bytes)
{
if ($bytes >= 1073741824)
{
$bytes = number_format($bytes / 1073741824, 2) . ' GB';
}
elseif ($bytes >= 1048576)
{
$bytes = number_format($bytes / 1048576, 2) . ' MB';
}
elseif ($bytes >= 1024)
{
$bytes = number_format($bytes / 1024, 2) . ' kB';
}
elseif ($bytes > 1)
{
$bytes = $bytes . ' bytes';
}
elseif ($bytes == 1)
{
$bytes = $bytes . ' byte';
}
else
{
$bytes = '0 bytes';
}
return $bytes;
}
$root = dirname("path");
function is_in_dir($file, $directory, $recursive = true, $limit = 1000) {
$directory = realpath($directory);
$parent = realpath($file);
$i = 0;
while ($parent) {
if ($directory == $parent) return true;
if ($parent == dirname($parent) || !$recursive) break;
$parent = dirname($parent);
}
return false;
}
$path = null;
if (isset($_GET['file'])) {
$path = $_GET['file'];
if (!is_in_dir($_GET['file'], $root)) {
$path = null;
} else {
$path = '/'.$path;
}
}
if (is_file($root.$path)) {
readfile($root.$path);
return;
}
echo "<div>\n";
echo "<table class=\"mdl-data-table mdl-js-data-table mdl-shadow--2dp\" style=\"min-width:300px\"\n";
echo " <thead>\n";
echo " <tr>\n";
echo " <th class=\"mdl-data-table__cell--non-numeric\">File</th>\n";
echo " <th>Size</th>\n";
echo " </tr>\n";
echo " </thead>\n";
echo " <tbody>";
if ($path) echo '<tr><td colspan="2" style="text-align:center"><a href="?file='.urlencode(substr(dirname($root.$path), strlen($root) + 1)).'">..</a></td></tr><br />';
foreach (glob($root.$path.'/*') as $file) {
$file = realpath($file);
$link = substr($file, strlen($root) + 1);
if (is_dir($file)){
echo '<tr><td style="text-align:center; vertical-align:middle"><a href="?file='.urlencode($link).'"><i class="material-icons" style="vertical-align:middle">folder</i></a></td><td style="text-align:center; vertical-align:middle"><span style="vertical-align:middle"><a href="?file='.urlencode($link).'">'.basename($file).'</a></span></td></tr><br />';
}
else {
$size = formatSizeUnits(filesize($file));
echo '<tr><td><a href="?file='.urlencode($link).'" download>'.basename($file).'</a></td><td>'.$size.'</td></tr><br />';
}
}
?>
</tbody>
</table>
</div>
</center>
</main>
</div>
</body>
</html>
我正在做的是更改$ root的值,但结果不是我所期望的,而不是允许我浏览它保留在根目录中的内容,即使我单击另一个文件夹。在我完成的一些测试中(使用不同的路径和权限),有时它甚至都没有显示任何内容。
任何人都可以告诉我为什么会发生这种情况以及如何解决这个问题? (请不要只用一个解决方案来回答,我想知道我想念和学习的是什么)
答案 0 :(得分:1)
我认为问题在于检索$ root路径。你的整个脚本都是基于使用绝对路径,所以我已经改变了$ root路径,也获得了它的绝对路径,并且它有效。
确保给 dirname 的参数确实存在,因为它看起来并不仅仅是字符串“path”。更多php.net
$root = realpath(dirname("path"));
顺便说一下: 在测试脚本时,我发现了有关下载文件的问题。单击文件时,它允许我下载它,但下载的文件包含脚本的HTML代码,直到文件发送的位置。因此,请确保将代码移动到文件的开头以便修复它。
if (is_file($root.$path)) {
readfile($root.$path);
return;
}
编辑:
我发现了关于函数 is_in_dir 的另一个问题,当根目录与脚本目录不完全相同时,它无法使用相对路径。为了让它工作,该函数需要在根目录$目录中查找指定的$文件,如下所示:
function is_in_dir($file, $directory, $recursive = true, $limit = 1000) {
$directory = realpath($directory);
// new:
$parent = realpath($directory . DIRECTORY_SEPARATOR . $file);
$i = 0;