PHP 显示/下载网络服务器根目录之外的目录文件

标签 php html directory

我已经下载并添加了这个非常简单的单文件 php web 文件浏览器系统(称为 Indexer)到我的 XAMPP 服务器。

我的 XAMMP 服务器在我的 C: 驱动器上,但我希望 Indexer 在我的 G: 驱动器上显示一个目录。但是当我更改(我认为是)正确的配置变量时,它无法正常工作。

这是我认为与问题有关的代码:

// configuration  
$Root = realpath("G:/test");  
$AllowDownload = TRUE;  
$WebServerPath = dirname("G:/test");

稍后在代码中...

elseif ($AllowDownload) {  

        echo "<a href=\"http://".getenv("SERVER_NAME").$WebServerPath."/$rel_path".$item["filename"]."\">".$item["name"]."</a>";
    }

事情是这样的:脚本确实正确显示了 G: 驱动器上“test”目录的内容,但是当我单击文件名以下载/查看文件时,链接被破坏是因为 php 构建了错误的链接(我想)。 链接如下所示:http://localhostg//[文件名]。

你知道如何解决这个问题吗?

如果我更改配置变量,此脚本将完美运行,以便它显示相关子目录的内容。它还说 $Root 变量可以位于网络服务器根目录之外。

此外,即使单击链接不起作用,右键单击并选择“目标另存为”也允许我保存/下载文件。

(如果您需要更多信息,请随时询问):)

最佳答案

您的网络服务器无法看到 DocRoot 之外的文件,因此它无法通过具有直接链接的浏览器提供文件。您需要使用 readfile() 将它们的内容打印到浏览器中标题设置正确。

要让它工作,您需要更改 indexer.php 中的配置:

// this way it works with accentuated letters in Windows
$Root = utf8_decode("G:\test"); // define the directory the index should be created for (can also be located outside the webserver root)

$AllowDownload = TRUE; // enclose file items with the anchor-tag (only makes sense when the files are in the webserver root)
// you need to place download.php in the same directory as indexer.php
$WebServerPath = dirname($_SERVER['SCRIPT_NAME']) . "/download.php?path="; // path where the indexed files can be accessed via a http URL (only required when $AllowDownload is TRUE)

并且您必须将名为 download.php 的新文件放置在与 indexer.php 相同的目录中,内容如下:

<?php

// it must be the same as in indexer.php
$Root = utf8_decode("G:\test");

function checkFileIsInsideRootDirectory($path, $root_directory) {
    $realpath = realpath($path);

    if (!file_exists($realpath))
        die("File is not readable: " . $path);

    // detects insecure path with for example /../ in it
    if (strpos($realpath, $root_directory) === false || strpos($realpath, $root_directory) > 0)
        die("Download from outside of the specified root directory is not allowed!");
}

function forceDownload($path) {
    $realpath = realpath($path);

    if (!is_readable($realpath))
        die("File is not readable: " . $path);

    $savename = (basename($path));

    header("Pragmaes: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private", false);
    header("Content-type: application/force-download");
    header("Content-Transfer-Encoding: Binary");
    header("Content-length: " . filesize($path));
    header("Content-disposition: attachment; filename=\"$savename\"");

    readfile("$path");
    exit;
}

if (!isset($_GET['path']))
    die("Path not specified!");

$fullPath = $Root . $_GET['path'];

checkFileIsInsideRootDirectory($fullPath, $Root);

forceDownload($fullPath);

关于PHP 显示/下载网络服务器根目录之外的目录文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4382219/

相关文章:

php - 在 Symfony2 中将 SecurityContext 注入(inject)监听器 prePersist 或 preUpdate 以获取 createdBy 或 updatedBy 中的用户导致循环引用错误

php - CKEDITOR - 在 Laravel 5 中使用 AJAX 更新/创建产品失败

php - spl_autoload_register 加载 PDO 时遇到问题?

jquery - 如何在 :after 范围内使用 JQuery CSS 从选择器动态更改颜色

php - 如何指定多个样式表

java - 无法使用 FileUtils 删除临时文件夹

Android 应用程序 - 如何写入 Android 设备的文档文件夹?

php - Paypal IPN 通知 : SSL Certificate Verify Failed

html - 这个 CSS3 发生了什么?我找不到它

android - 如何在 Android 中以编程方式获取铃声 "/system/media/audio/ringtones"的路径?