I spent already over two days on this problem. It looks that PHP function connection_aborted() does not work reliably. Sometimes it reports aborted connection with the client properly, but in about 50% it does not report the abort.
Is there any other way to detect aborted connection with the client from within the PHP script, please? Maybe establishing socket connection or anything else?
Sample code:
This is my latest version of the file downloading code. Downloading works properly even on large files, but connection_aborted()
works only occasionally.
// disable script abort
ignore_user_abort(true);
// never expire this download script
set_time_limit(0);
while(!feof($fileObject) && (connection_status()==0))
{
//usleep(100000);
print(@fread($fileObject, $chunkSize));
// gradually output buffer to avoid memory problems by downloading large files
ob_flush();
flush();
$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
exit(json_encode(array("result" => false, "error" => "Error Processing Database Query.")));
}
}
// check if the client was disconnected
// important for cancelled or interrupted downloads
if ( (connection_status()!=0) || (connection_aborted()!=0) )
{
// empty the output buffer
ob_flush();
flush();
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.")));
}
Thank you and kind regards.