简体   繁体   English

在PHP中引用外部驱动器

[英]Referencing an external drive in PHP

Okay, so i am trying to start hosting my own file sharing site, i currently am using WAMP server to have server properties with apache, PHP, mysql, etc. I have my root folder located in a 2TB hard drive and i would like to list folders/files in another hard drive. 好的,所以我试图开始托管我自己的文件共享站点,我目前正在使用WAMP服务器来具有apache,PHP,mysql等服务器属性。我的根文件夹位于2TB硬盘中,我想列出另一个硬盘中的文件夹/文件。 But when i use the dir function it does not link to the actual files it just lists them. 但是,当我使用dir函数时,它不会链接到实际文件,只会列出它们。 I would like to allow people to download the files in my HDD to there client computers. 我想允许人们将HDD中的文件下载到那里的客户端计算机。 Any ideas on how to work this out and link to the actual files? 关于如何解决这个问题并链接到实际文件的任何想法?

You can't link to files that are not in your website directly. 您无法直接链接到不在您的网站中的文件。

What you can do is have all the links point to a download script that takes a parameter which points to the file. 您可以做的是让所有链接都指向一个下载脚本,该脚本带有一个指向文件的参数。 That script can then make it work in memory. 然后,该脚本可以使其在内存中工作。

Here's an example I found on the web here: 这是我在网上找到的示例:
http://www.finalwebsites.com/forums/topic/php-file-download 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>

In linux i would create a symbolic link from the external hard drive into you webroot folder, but under windows it looks like you need to create a junction directory. 在Linux中,我会创建一个从外部硬盘驱动器到您的webroot文件夹的符号链接,但是在Windows下,您似乎需要创建一个联结目录。

Have a read of this, it explains how to create it. 阅读此书,它说明了如何创建它。

http://www.howtogeek.com/howto/windows-vista/using-symlinks-in-windows-vista/ http://www.howtogeek.com/howto/windows-vista/using-symlinks-in-windows-vista/

Your directory structure should end up looking like this 您的目录结构应最终看起来像这样

webroot
|- php files
|- externalfiles (dir junction to ext hard drive)
   |- sharedfile1 
   |- sharedfile2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM