【问题标题】:Disable direct downloads禁用直接下载
【发布时间】:2014-09-15 11:53:57
【问题描述】:

我正在开发一个使用 PHP 开发的高安全性系统。有一些文件由用户上传并上传到目录。我怎样才能做到这一点,这样你就不能只去文件的 url 并下载它?不过,我仍然需要能够从网站的管理部分下载该文件。

谢谢

【问题讨论】:

  • 在网络路由之外创建目录。

标签: php apache file download


【解决方案1】:

有几种方法可以做到这一点,一种是通过.htaccess文件点击上传文件的目录,其中包含:

order deny,allow
deny from all

之后,当您想要下载文件时(假设您将文件路径保存在数据库中的某个位置,或者您可以在 GET 参数中以某种形式传递它们),您可以使用类似于此的代码下载它们(以及基于 PHP 文档的示例):

// check user credentials
check_if_logged_in();

// get path to file
$file = get_your_file_path();

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

【讨论】:

    【解决方案2】:

    将文件放在 web 目录之外。然后用这个下载:how to access file from outside root directory in php

    【讨论】:

      猜你喜欢
      • 2012-07-24
      • 1970-01-01
      • 1970-01-01
      • 2023-02-08
      • 1970-01-01
      • 2020-06-01
      • 2021-04-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多