2011-10-22 39 views
0

好吧,所以我想开始托管我自己的文件共享网站,我目前正在使用WAMP服务器具有Apache,PHP,MySQL等服务器属性。我有我的根文件夹位于一个2TB硬盘驱动器,我想列出另一个硬盘驱动器中的文件夹/文件。但是当我使用dir函数时,它并没有链接到它列出的实际文件。我希望允许用户将硬盘中的文件下载到客户端计算机上。任何想法如何解决这个问题,并链接到实际的文件?引用外部驱动器在PHP

+0

你能告诉一个代码示例,它输出什么? –

+1

您可以在您的webroot内创建一个连接点到该驱动器的目录。 – hakre

回答

0

您无法直接链接到不在您网站上的文件。

你可以做的就是让所有的链接指向一个下载脚本,它需要一个指向该文件的参数。该脚本可以使其在内存中工作。

下面是一个例子,我的网站在这里找到:
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> 
+0

我做了一个新的PHP页面,并将其放置在我的根文件夹中,但是当我打开它时,它给了我3个错误,我使用了准确的代码,给了我一些小小的争执,但它仍然不会工作。我的根目录是:F:/ wamp/www/nfs /。我试图从位于字母P:/的另一个驱动器中引用或下载文件。这是说,我的错误是在12,14和42行。 –

+0

这里是我在测试你提供的代码时得到的错误图片: [img] http://bayimg.com/dAKNHAaDJ [img] –

+0

你将不得不开始调试。第一个错误是参考“未定义索引:download_file”。这意味着顶部的get没有名为download_file的参数。代码片段底部的链接显示了如何构建链接,以便$ _GET数组拥有download_file的密钥。 – evan

0

在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