【问题标题】:Download handler (php code) is called multiple times for a single request单个请求多次调用下载处理程序(php 代码)
【发布时间】:2020-03-31 05:08:38
【问题描述】:

我有一个处理下载请求的 PHP 脚本。客户端可以使用这样的 GET 请求(Javascript)访问此脚本:

if (true)
{
  window.open('/download?mode=paper&fid=' + fid, '_blank');
  return;
}

这里是(简化的)处理程序:

function downloadPaper($fid)
{
    // Adds a record to the database, but it seems to be called multiple times
    Daban\PaperTools::addPaperDownloadRecord($fid);

    header('Content-Description: File Transfer');
    header("Content-Type: application/octet-stream");
    header("Content-Disposition: attachment; filename=$fakeFileName");
    header('Content-Transfer-Encoding: binary');
    header('Connection: Keep-Alive');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header("Content-Length: " . filesize($file));

    $fh = fopen($file, "rb");
    while (!feof($fh))
    {
        echo fgets($fh);
        flush();
    }
    fclose($fh);
}

问题是根据数据库,每次下载都有多条下载记录。换句话说,处理程序会为单个请求多次调用(或者至少我认为是这样)。 附言addPaperDownloadRecord() 只是一些 SQL 代码,它不是罪魁祸首。我还检查了客户端,它只发送一次。

【问题讨论】:

  • 你确定这是多次调用的handle(尝试添加一些echo,看看它被调用了多少次)?或者它可能是方法addPaperDownloadRecord
  • 首先使用浏览器开发工具网络面板验证客户端实际发出了多少请求。

标签: javascript php get-request


【解决方案1】:

我设法找到了原因。如果有人有类似的问题,这可能会有所帮助。由于我的系统中有一个下载管理器,它会发送多个请求来分割文件以加快下载过程。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-26
    • 2013-06-03
    • 2017-04-28
    • 1970-01-01
    • 2013-07-16
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    相关资源
    最近更新 更多