V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
xuexixuexi2
V2EX  ›  问与答

请教一个 nginx 做下载服务器,关于统计的问题。

  •  
  •   xuexixuexi2 · 2017-01-11 14:59:31 +08:00 · 1584 次点击
    这是一个创建于 2663 天前的主题,其中的信息可能已经有所发展或是发生改变。

    需求:需要知道有人开始下载,是否下载完成。
    思路:通过 php 的 fpassthru 发送实际的文件数据,并在开始前和结束后写数据库。
    代码:

    $db = new DownloadDB();
    $db->exec("INSERT INTO tblDownloads(info) VALUES('下载开始');");
    $db->close();
    
    $filepath = '../files/file1';
    @header('Content-type: application/octet-stream');
    @header('Content-Disposition: attachment; filename=' . $filename);
    @header('Accept-Ranges: bytes');
    @header('Content-Length: ' . filesize($filepath));
    @header('Cache-control: no-cache,no-store,must-revalidate');
    @header('Pragma: no-cache');
    @header('Expires: 0');
    $file = @fopen($filepath, "rb");
    @fpassthru($file);
    @fclose($file);
    
    $db = new DownloadDB();
    $db->exec("INSERT INTO tblDownloads(info) VALUES('下载完成');");
    $db->close();
    

    问题:还没有下载完,就写了“下载完成”到数据库中了,甚至是和“下载开始”同时写入数据库的。
    tblDownloads 表有时间字段,可以看到下载完成和下载开始是同时写入数据库的,也就是中间的 fopen-fpassthru-fclose 这些耗时为 0 。
    而我的理解是 fpassthru 应该将数据全部推送给用户后才返回,是不是有缓存机制?
    环境: Linux+nginx+fastCGI

    1 条回复    2017-01-12 09:16:10 +08:00
    firefox12
        1
    firefox12  
       2017-01-12 09:16:10 +08:00
    这么做得死, 要么用模块,要么用日志
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3590 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 04:35 · PVG 12:35 · LAX 21:35 · JFK 00:35
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.