
需求:需要知道有人开始下载,是否下载完成。
思路:通过 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 firefox12 2017-01-12 09:16:10 +08:00 这么做得死, 要么用模块,要么用日志 |