在 PHP 中引用外部驱动器

发布于 2024-12-11 22:26:12 字数 202 浏览 0 评论 0原文

好的,所以我正在尝试开始托管我自己的文件共享网站,我目前正在使用 WAMP 服务器来拥有 apache、PHP、mysql 等服务器属性。我的根文件夹位于 2TB 硬盘驱动器中,我想列出另一个硬盘驱动器中的文件夹/文件。但是当我使用 dir 函数时,它不会链接到实际文件,它只是列出它们。我想允许人们将我的硬盘中的文件下载到客户端计算机上。关于如何解决这个问题并链接到实际文件有什么想法吗?

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. But when i use the dir function it does not link to the actual files it just lists them. I would like to allow people to download the files in my HDD to there client computers. Any ideas on how to work this out and link to the actual files?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2024-12-18 22:26:12

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

您可以做的就是让所有链接都指向一个下载脚本,该脚本采用指向该文件的参数。然后该脚本可以使其在内存中运行。

这是我在网上找到的一个示例:
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>

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

// 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>
注定孤独终老 2024-12-18 22:26:12

在 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

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.

Have a read of this, it explains how to create it.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文