问题描述
我已经下载并添加了这个非常简单的一个文件,php web文件浏览器系统(称为)到我的XAMPP服务器。
我的XAMMP服务器位于我的C:驱动器上,但是我希望Indexer在我的G:驱动器上显示一个目录。但是,当我改变(我认为是)正确的配置变量时,它不能正常工作。
这是我认为是解决问题的代码:
//配置
$ Root = realpath(G:/ test);
$ AllowDownload = TRUE;
$ WebServerPath = dirname(G:/ test);
及以后的代码...
elseif($ AllowDownload){
echo< a href = \http://.getenv(SERVER_NAME)$ WebServerPath / $ rel_path $项[ 文件名] \ > [名 ]。 < $项目。; / A> 中;
}
发生这种情况:脚本正确地在G:驱动器上显示test目录的内容,但是当我单击文件名下载/查看文件时,链接被破坏,因为php构造链接错误(我想)。
链接如下所示:http:// localhostg // [文件名]
你会知道如何解决这个问题吗? >
如果我更改配置变量,以便显示相对子目录的内容,则此脚本工作正常。而且还说$ Root变量可以位于webserver根外部。
此外,即使点击链接不起作用,右键单击并选择另存目标可以保存/下载文件。
(随意询问是否需要更多信息):)
Web服务器看不到DocRoot以外的文件,因此无法通过直接链接的浏览器提供文件。您需要使用,标题正确设置。
要使此工作正常,您需要更改indexer.php中的配置:
//这样它可以与Windows
$ Root = utf8_decode(G:\test)中的加密字母一起使用; //定义索引应该创建的目录(也可以位于webserver根目录之外)
$ AllowDownload = TRUE; //包含锚标签的文件项(仅当文件位于Web服务器根目录中时才有意义)
//您需要将download.php放在与indexer.php相同的目录中
$ WebServerPath = dirname($ _ SERVER ['SCRIPT_NAME'])。 /download.php?path=; //通过http URL访问索引文件的路径(仅当$ AllowDownload为TRUE时才需要)
而且您必须在与 indexer.php
相同的目录中放置一个名为 download.php
的新文件,其中此内容:
<?php
//它必须与索引器中相同。 php
$ Root = utf8_decode(G:\test);
函数checkFileIsInsideRootDirectory($ path,$ root_directory){
$ realpath = realpath($ path);
if(!file_exists($ realpath))
die(File is not readable:$ path);
//检测不安全的路径,例如/../ in
if(stratus($ realpath,$ root_directory)=== false || strpos($ realpath,$ root_directory) > 0)
die(从指定的根目录外部不允许下载!);
}
函数forceDownload($ path){
$ realpath = realpath($ path);
如果(!is_readable($ realpath))
die(File is not可读:$ 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);
退出;
}
如果(!isset($ _ GET ['path']))
die(Path not specified!);
$ fullPath = $ Root。 $ _GET [路径];
checkFileIsInsideRootDirectory($ fullPath,$ Root);
forceDownload($ fullPath);
I have downloaded and added this very simple, one file, php web file explorer system(called Indexer) to my XAMPP server.
My XAMMP server is on my C: drive, but I want Indexer to display a directory on my G: drive. But when I change (what I think are) the right configuration variables, it doesn't work properly.
Here is the code I think is to do with the problem:
// configuration
$Root = realpath("G:/test");
$AllowDownload = TRUE;
$WebServerPath = dirname("G:/test");
and later on in the code...
elseif ($AllowDownload) {
echo "<a href=\"http://".getenv("SERVER_NAME").$WebServerPath."/$rel_path".$item["filename"]."\">".$item["name"]."</a>";
}
This is what happens: The script does correctly display the contents of the "test" directory on the G: drive, but when I click the filename, to download/view the file, the link is broken because the php constructs the link wrong (I suppose).The link looks like this: http://localhostg//[name of file].
Would you know how to solve this problem?
This script works perfectly if I change the configuration variables so it displays the contents of a relative subdirectory. And it also says $Root variable can be located outside the webserver root.
Also, even though clicking the link doesn't work, right-clicking and selecting "Save Target As" allows me to save/download the file.
(Feel free to ask if you need more information) :)
Your web server can not see the files outside the DocRoot, so it can not serve the files via the browser with direct links. You need to print their contents into the browser with readfile()
with the headers properly set.
To make this work, you need to change the configuration in 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)
And you have to place a new file called download.php
in the same directory as indexer.php
, with this content:
<?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显示/下载webserver根目录下的目录文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!