【问题标题】:Secure file download in PHP, deny user without permissionPHP中的安全文件下载,未经许可拒绝用户
【发布时间】:2012-11-19 12:44:14
【问题描述】:

我正在创建一个网站,您可以在其中购买虚拟积分文件,然后下载它们。我不想让用户在不购买的情况下下载文件,所以我必须隐藏它们。我将所有文件放在一个文件夹中,未经主机以外的任何人许可,问题是当有人购买文件并想要下载它时。

我决定制作一个文件获取器,它将检查用户的权限,然后打印出文件内容。到目前为止我的代码:

<?php
    require_once('content/requirements.php'); //mysql connections
    secure(1); //disconnect unlogged users

    if (!isset($_GET['id'])) //if no file id provided
        die();

    $fid=mysql_real_escape_string($_GET['id']); //file id

    $query="SELECT * FROM files WHERE user_id = "$_SESSION['user_id']." AND file_id = ".$id;

    $q=mysql_query($query);

    if (mysql_num_rows($q)!=1) //if no permission for file or multipe files returned
        die();

    $file=mysql_fetch_array($q); //file id
    $sub=mysql_fetch_array(mysql_query("SELECT * FROM sub WHERE id = ".$file['file_id'])); //payment id
?>

现在,当我确定用户有权执行此操作时,phpScript 应该写入文件内容并发送适当的标头以让用户下载它。

如何逐字节读取文件并打印它以及我应该在 header() 中写入什么以使文件可下载(因此您不必复制粘贴其内容)。

也许这不是最好的方法,但这是我一段时间以来想到的最好的事情。

感谢任何帮助。

【问题讨论】:

  • 使用谷歌。对此有数百万种解释。
  • 尝试在谷歌上搜索“SQL 注入”。

标签: php file download


【解决方案1】:

来自readfile php doc

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

【讨论】:

  • 这不是安全脚本。站点用户也可以通过相对路径下载其他文件。
【解决方案2】:

【讨论】:

  • 我实现了芯片的教程,效果很好。谢谢
  • 嗨,我收到了一个损坏的 zip 文件,经过挑选和戳穿后,我找到了解决方案。同样,从 tutorialchip 类中,在 ob_clean 之前的某个时间添加 ob_start() 以防止 zip 文件出现错误 "
    Notice: ob_clean() [ref. outcontrol]: 删除缓冲区失败。在 /some/grid/server...413
    " 中没有要删除的缓冲区跨度>
  • 找到的解决方案:它不采用绝对路径,例如“example.com/directory/filename.exe”,您必须使用您的 download.php 文件的相对路径,即“directory/filename.exe”应该是您的 dpwnload 路径。 .
  • webinfopedia.com/php-file-download-script.html 我的 sql 这个网站出错了。
猜你喜欢
  • 2018-02-06
  • 2016-09-21
  • 1970-01-01
  • 1970-01-01
  • 2011-05-30
  • 2012-08-18
  • 2012-06-18
  • 2013-11-14
  • 1970-01-01
相关资源
最近更新 更多