PHP显示/下载Web服务器根目录以外的目录文件
我已经下载了这个非常简单的单文件php web文件浏览器系统(称为),并将其添加到我的XAMPP服务器中 我的XAMMP服务器在我的C:驱动器上,但我希望Indexer在我的G:驱动器上显示一个目录。但是当我改变(我认为是)正确的配置变量时,它就不能正常工作 以下是我认为与此问题相关的代码:PHP显示/下载Web服务器根目录以外的目录文件,php,html,directory,Php,Html,Directory,我已经下载了这个非常简单的单文件php web文件浏览器系统(称为),并将其添加到我的XAMPP服务器中 我的XAMMP服务器在我的C:驱动器上,但我希望Indexer在我的G:驱动器上显示一个目录。但是当我改变(我认为是)正确的配置变量时,它就不能正常工作 以下是我认为与此问题相关的代码: // configuration $Root = realpath("G:/test"); $AllowDownload = TRUE; $WebServerPath = dirname("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>";
}
elseif($AllowDownload){
回声“;
}
事情就是这样:脚本确实正确地显示了G:drive上“test”目录的内容,但是当我单击文件名下载/查看文件时,链接被破坏了,因为php构造了错误的链接(我想)。
链接如下所示:http://localhostg//[文件名]
你知道怎么解决这个问题吗
如果我更改配置变量,使其显示相对子目录的内容,那么该脚本将非常有效。它还说$Root变量可以位于Web服务器根之外
此外,即使单击链接不起作用,右键单击并选择“将目标另存为”允许我保存/下载文件
(如果您需要更多信息,请随时询问):您的web服务器无法查看DocRoot之外的文件,因此它无法通过具有直接链接的浏览器提供文件。您需要将其内容打印到浏览器中,并正确设置标题 要实现这一点,您需要更改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);
您的web服务器无法看到DocRoot之外的文件,因此无法通过浏览器提供直接链接的文件。您需要将其内容打印到浏览器中,并正确设置标题
要实现这一点,您需要更改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);
让我们看看这个脚本:
$Root = realpath("."); // 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)
$WebServerPath = dirname(getenv("SCRIPT_NAME")); // path where the indexed files can be accessed via a http URL (only required when $AllowDownload is TRUE)
注意“仅当文件位于Web服务器根目录中时才有意义”和“可通过http URL访问索引文件的路径”。这表明此脚本设计为无法下载web服务器根目录之外的文件
但是,您可以修改此脚本,使其能够按照styu在其回答中指出的方式执行。然后,您可以将更改发送给脚本作者
顺便说一句,我在自己的服务器上测试了这个脚本。让我们看看这个脚本:
$Root = realpath("."); // 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)
$WebServerPath = dirname(getenv("SCRIPT_NAME")); // path where the indexed files can be accessed via a http URL (only required when $AllowDownload is TRUE)
注意“仅当文件位于Web服务器根目录中时才有意义”和“可通过http URL访问索引文件的路径”。这表明此脚本设计为无法下载web服务器根目录之外的文件
但是,您可以修改此脚本,使其能够按照styu在其回答中指出的方式执行。然后,您可以将更改发送给脚本作者
顺便说一句,我在自己的服务器上进行了测试。您必须更改apache配置。问题不在于php脚本,而在于web服务器(除非您将其配置为web根目录,否则无法为web根目录之外的文件提供服务)
在apache配置中尝试以下操作:
Alias /testalias "G:/test"
<Directory "G:/test">
Options Indexes FollowSymLinks MultiViews ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
它应该会起作用 您必须更改apache配置。问题不在于php脚本,而在于web服务器(除非您将其配置为web根目录,否则无法为web根目录之外的文件提供服务)
在apache配置中尝试以下操作:
Alias /testalias "G:/test"
<Directory "G:/test">
Options Indexes FollowSymLinks MultiViews ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
它应该会起作用 谢谢你的回答!这听起来很有希望;我需要把这段代码放在哪里(以及我需要更改任何变量名)?编辑了我的答案,现在它可以正常工作。感谢您的持续支持:)我应该把代码放在哪里,它需要替换什么?@Jopper正如我写的,您只需要更改indexer.php中的配置参数(事实上,只有$WebServerPath
被更改为破解下载url),您需要创建一个新的php脚本来处理下载,并且必须根据$WebServerPath
的值(在本例中为download.php)来命名。也可以将两个文件合并为一个,但这种方式似乎更简单(我不想修改indexer.php)。感谢您的回答!这听起来很有希望;我需要将此代码放在哪里(以及我需要更改任何变量名)?编辑了我的回答,现在它可以正常工作。感谢您的持续支持:)我应该把代码放在哪里,需要替换什么?@Jopper在我写的时候,您只需要更改indexer.php中的配置参数(实际上只有$WebServerPath
被更改为破解下载url),您需要创建一个新的php脚本来处理下载,并且必须根据$webServerPath
的值(在本例中为download.php)对其进行命名。也可以将两个文件合并为一个文件,但这种方式似乎更简单(我不想修改indexer.php)。我在XAMPP Apache config(conf)文件夹中找到了一个名为“httpd.conf”的文件。我想这是正确的文件,但我是将代码(从您的第一个代码框)粘贴到该文件中,还是覆盖了某个文件?另外,我是否需要更改“testalias”的名称关于Indexer.php文件所在的文件夹的名称?只是一个建议:它可以正常工作,除非您的目的是保护您的文件(将它们放在webroot之外)。我在XAMPP Apache config(conf)文件夹中找到一个名为“httpd.conf”的文件。我想这是正确的文件,但我是否只粘贴代码(从您的第一个代码框中粘贴)我是否需要将“testalias”的名称更改为