php - 为什么我不能更改根目录?

标签 php html css php-7

我正在构建目录浏览器,在寻找信息时我找到了 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 的值,但结果不是我所期望的,而不是允许我浏览它保留在根目录中的内容,即使我单击另一个文件夹。在我所做的一些测试中(使用不同的路径和权限)有时它甚至没有显示任何内容。

谁能告诉我为什么会这样以及如何解决这个问题? (请不要只回答一个解决方案,我想知道我误解和学习的是什么)

最佳答案

我认为问题在于检索 $root 路径。您的整个脚本基于使用绝对路径,因此我更改了 $root 路径以也获取它的绝对路径并且它起作用了。

确保给 dirname 的参数确实存在,因为它看起来不只是字符串“路径”。更多信息 php.net

$root = realpath(目录名("路径"));

顺便说一句: 在测试您的脚本时,我发现有关下载文件的问题。单击文件时,它允许我下载它,但下载的文件包含脚本的 HTML 代码,直到发送文件为止。因此,请确保将您的代码移动到文件的开头以便修复它。

if (is_file($root.$path)) {
    readfile($root.$path);
    return;
}

编辑:

我发现另一个关于函数 is_in_dir 的问题,当根目录与脚本目录不完全相同时,它无法使用相对路径。为了让它工作,该函数需要在根目录 $directory 中查找指定的 $file,如下所示:

    function is_in_dir($file, $directory, $recursive = true, $limit = 1000) {
        $directory = realpath($directory);
        // new:
        $parent = realpath($directory . DIRECTORY_SEPARATOR . $file);
        $i = 0;

关于php - 为什么我不能更改根目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43573935/

相关文章:

html - 如果 div 不存在,您可以在 CSS 中应用 CSS 规则吗?

javascript - 如何获取/查找从 javascript 或 jquery 中的 DOM 元素或变量调用的函数

html - 强制文本在浏览器中换行的方法,即使它是一个没有空格的连续字符串?

php - 将 2 个表连接在一起并返回匹配的所有数据(使用 MySQL 的 PHP/Laravel5.2)

php - Mysql查询Sum with Case

php - HTMl 表格的边框未显示在 Php PDF 上

php - fatal error :消息 'Zend_Gdata_App_HttpException'未被捕获的异常 'Expected response code 200, got 401'

html - 在 chrome 和 firefox 中渲染不同

html - CSS3无限循环回到第一张图片

python - 如何从 HTML 获取 Python 数组的第一个元素