2010-12-07 140 views
1

我已經下載並將其添加到我的XAMPP服務器,這非常簡單,一個文件,php web文件瀏覽器系統(稱爲Indexer)。PHP在Web服務器根目錄之外顯示/下載目錄文件

我的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:驅動器上的「測試」目錄的內容,但是當我點擊文件名,下載/查看文件時,由於PHP構造鏈接錯誤(我想),鏈接被破壞。 鏈接如下所示:http:// localhostg // [文件名]。

你知道如何解決這個問題嗎?

如果我更改配置變量,使其顯示相對子目錄的內容,該腳本完美地工作。它還表示$ root變量可以位於Web服務器根目錄之外。

此外,即使點擊鏈接不起作用,右鍵單擊並選擇「另存目標」允許我保存/下載文件。

(隨意問,如果你需要了解更多信息):)

回答

4

您的Web服務器無法看到文檔根外的文件,因此無法通過直接鏈接瀏覽器提供文件服務。您需要將其內容打印到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) 

而且你必須放置在同一目錄indexer.php稱爲download.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); 
+0

感謝您的回答!這聽起來有希望;我需要把這個代碼放在哪裏(並且我需要更改任何變量名稱)? – Jopper 2010-12-08 17:03:52

+0

編輯我的答案,現在它適用於[`indexer`](http://tbmnet.de/tbmnet.php?content=php_indexer)。 – 2010-12-08 21:00:08

1

讓我們來看看這個腳本:

$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在他的答案中指出的方式執行此操作。然後,您可以將更改發送給腳本的作者。

順便說一句,我測試了這個在我自己的服務器上。

2

您必須更改您的apache配置。問題不在於PHP腳本,問題在於網絡服務器(除非您將網絡服務器配置爲無法在Web根目錄之外提供文件)。

嘗試這樣的事情在你的Apache配置:

Alias /testalias "G:/test" 
<Directory "G:/test"> 
    Options Indexes FollowSymLinks MultiViews ExecCGI 
    AllowOverride All 
    Order allow,deny 
    Allow from all 
</Directory> 

這告訴阿帕奇給G提供文件:/測試,當你訪問http://localhost/testalias

然後改變一樣,您的腳本配置:

$WebServerPath = dirname("testalias"); 

它應該工作!

相關問題