【问题标题】:Download file from NAS attached to server through http通过 http 从连接到服务器的 NAS 下载文件
【发布时间】:2018-05-23 16:03:01
【问题描述】:

我有许多文件存储在 NAS 中。 NAS 作为网络驱动器连接到服务器(假设它位于 Y://)。

我使用 xampp 来为我在 php 中构建的应用程序提供服务。该应用程序旨在为用户提供从 NAS 下载文件的服务,直接通过 http 而不是 ftp。

那么我可以设置来自 NAS 的文件,以便使用 http URL 下载,例如 example.com/files/the-file.zip 吗?

xampp位于C://目录

注意:xampp htdocs 已经设置为由域访问。所以这不是域指向问题

【问题讨论】:

标签: php nas


【解决方案1】:

你可以通过 PHP 试试:

<?php
$downloadFolder = 'Y:/';

$fileName = $downloadFolder . $_GET['file'];
$sanitizedFileName = realpath($fileName);

if($fileName !== $sanitizedFileName) {
  throw new RuntimeException('Someone tried to escape');
}

// As seen in http://php.net/readfile:
if (file_exists($fileName)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="'.basename($fileName).'"');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($fileName));
    readfile($fileName);
    exit;
}

或者找到一种通过 .htaccess 运行所有内容的方法,但这可能会减少您对安全处理的控制(可能您不想从其他目录或其他驱动器提供文件)

【讨论】:

  • 谢谢!但我只是好奇检查已清理文件的功能是什么?因为我总是得到那个错误。但是在我评论那行之后,文件就被下载了。但它已损坏。那么现在的问题是什么?仅供参考,文件很大(以 GB 为单位)
  • 啊!现在可以了!我在 readfile() 之前和之后添加 ob_get_clean() 和 ob_flush()
  • 我们需要在 $downloadFolder 中添加双反斜杠,而不是单斜杠。现在可以了!
猜你喜欢
  • 2015-05-09
  • 1970-01-01
  • 1970-01-01
  • 2014-08-25
  • 1970-01-01
  • 1970-01-01
  • 2013-05-29
  • 1970-01-01
  • 2020-12-18
相关资源
最近更新 更多