PHP Connection_Aborted() 仅有时有效 [英] PHP Connection_Aborted() Works Only Sometimes
问题描述
我有文件下载 PHP 脚本.该脚本可以下载最大 4GB+ 的大文件.在现实中,经常会发生用户在下载过程中取消下载过程或关闭浏览器窗口的情况.
I have file download PHP script. The script can download large files up to 4GB+. In reality, it will often happen that users will cancel download process or will close browser window while downloading.
因此,我必须在已经开始的下载过程因任何原因中止时进行注册.对此的最佳解决方案似乎是通过 connection_aborted() 函数.
I have to, therefore, register when the already started download process will have been for any reason aborted. The optimal solution for this looks to be monitoring connection via connection_aborted() function.
'connection_aborted()' 似乎对下载取消或关闭浏览器窗口有反应.我的问题是,它不会以 100% 的精度做出反应.它记录了大约 50% 的取消下载或关闭的浏览器.如果未检测到连接中止,则下载只会在服务器上继续,就好像浏览器不会取消一样.
'connection_aborted()' seems to react on download cancel or closing my browser window. My problem is, that it does not react with 100% precision. It registers about 50% of cancelled downloads or closed browsers. If the connection abort is not detected, the download simply continues on the server as if the browser would not cancel it.
请您检查我的代码是否存在漏洞和错误?我需要了解导致这种行为的原因:
Would you, please, review my code for vulnerabilities and errors? I need to understand, what causes the behavior:
// empty and turn off output buffering
ob_flush();
flush();
// never expire this download script
set_time_limit(0);
fseek($fileObject, $seek_start);
while(!feof($fileObject))
{
//usleep(100000);
//print(@fread($fileObject, $chunkSize));
echo(@fread($fileObject, $chunkSize));
// gradually output buffer to avoid memory problems by downloading large files
ob_flush();
flush();
// check if the client was disconnected
// important for cancelled or interrupted downloads
if (Connection_Aborted())
{
ChromePhp::log("Connection Aborted");
// sent to the database that the connection has been aborted
$result = mysqli_query($dbc, "UPDATE current_downloads SET connection_aborted=TRUE WHERE user_id=1;");
// close the database connection
mysqli_close($dbc);
// close the open file
@fclose($fileObject);
exit(json_encode(array("result" => false, "error" => "Connection with the client was aborted.")));
}
$nLoopCounter++;
$transferred += $chunkSize;
$downloadPercentage = (($nLoopCounter * $chunkSize) / $fileSize) * 100;
$result = mysqli_query($dbc, "UPDATE current_downloads SET progress_percent=$downloadPercentage, transferred=$transferred, connection_aborted=$strConnectionAborted, iteration=$nLoopCounter WHERE user_id=1;");
if($result == false)
{
// close the database connection
mysqli_close($dbc);
// close the file
fclose($handle);
// prepare output message
$outputArray = array("result" => 0, "message" => "Error Processing Database Query");
// output the message
echo json_encode($outputArray);
exit;
}
}
// file save was a success
@fclose($fileObject);
我使用以下内容:
- Apache 2.4.4
- PHP 5.4.12
- MySQL 5.6.12
- 谷歌浏览器版本 32.0.1700.107 米
- Windows 7 x64
谢谢.
推荐答案
添加函数connection_status
,然后再试
if(connection_status()!=0||connection_aborted()!=0||){
//your code
}
这篇关于PHP Connection_Aborted() 仅有时有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!