[英]Referencing an external drive in PHP
好的,所以我試圖開始托管我自己的文件共享站點,我目前正在使用WAMP服務器來具有apache,PHP,mysql等服務器屬性。我的根文件夾位於2TB硬盤中,我想列出另一個硬盤中的文件夾/文件。 但是,當我使用dir函數時,它不會鏈接到實際文件,只會列出它們。 我想允許人們將HDD中的文件下載到那里的客戶端計算機。 關於如何解決這個問題並鏈接到實際文件的任何想法?
您無法直接鏈接到不在您的網站中的文件。
您可以做的是讓所有鏈接都指向一個下載腳本,該腳本帶有一個指向文件的參數。 然后,該腳本可以使其在內存中工作。
這是我在網上找到的示例:
http://www.finalwebsites.com/forums/topic/php-file-download
// place this code inside a php file and call it f.e. "download.php"
$path = $_SERVER['DOCUMENT_ROOT'] . "/path2file/"; // change the path to fit your websites document structure
$fullPath = $path . $_GET['download_file'];
if ($fd = fopen ($fullPath, "r"))
{
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
switch ($ext)
{
case "pdf":
header("Content-type: application/pdf"); // add here more headers for diff. extensions
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
break;
default:
header("Content-type: application/octet-stream");
header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
break;
}
header("Content-length: $fsize");
header("Cache-control: private"); //use this to open files directly
while(!feof($fd))
{
$buffer = fread($fd, 2048);
echo $buffer;
}
}
fclose ($fd);
exit;
// example: place this kind of link into the document where the file download is offered:
// <a href="download.php?download_file=some_file.pdf">Download here</a>
在Linux中,我會創建一個從外部硬盤驅動器到您的webroot文件夾的符號鏈接,但是在Windows下,您似乎需要創建一個聯結目錄。
閱讀此書,它說明了如何創建它。
http://www.howtogeek.com/howto/windows-vista/using-symlinks-in-windows-vista/
您的目錄結構應最終看起來像這樣
webroot
|- php files
|- externalfiles (dir junction to ext hard drive)
|- sharedfile1
|- sharedfile2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.