我需要记录特定文件的总下载量 . 下载功能工作正常,但无法定义用户是否取消(在浏览器对话框中单击"cancel")或后面的连接是否中止 .
我理解's not simple to know when a file download was finished, so I'我想通过两种方式来解决这个问题 . 无效:
-
Get total bytes sent, latter I will compare it with total file size: 这样$ bytes_sent var总是设置为总文件大小,无论用户单击下载对话框的取消按钮还是后取消下载过程 .
-
Trigger connection_aborted() function: 没有找到此功能发生的方式并定义我的会话var ...
(如果我与 Session 合作的事实是相关的,那我就不会感到害羞) .
我感谢您的帮助 :)
<?php
if(is_file($filepath)){
$handle = fopen($filepath, "r");
header("Content-Type: $mime_type");
header("Content-Length: ". filesize($filepath).";");
header("Content-disposition: attachment; filename=" . $name);
while(!feof($handle)){
ignore_user_abort(true);
set_time_limit(0);
$data = fread($handle, filesize($filepath));
print $data;
$_SESSION['download'] = 'Successful download';
//Always is set as total file lenght, even when cancel a large file download before it finish:
bytes_sent = ftell($handle);
flush();
ob_flush();
//Can't trigger connection aborted, in any case:
if(connection_aborted()){
$_SESSION['download'] = 'Canceled download';
}
}
}
PHP版本5.3.29
1 回答
您需要以小块读取文件,而不是一次性读取所有文件 .