PHP Connection_Aborted () only works sometimes

I have a file to load a PHP script. The script can upload large files up to 4 GB +. In fact, it often happens that users will cancel the download process or close the browser window during the download.

Therefore, I must register when the already started download process is interrupted for any reason. The best solution for this is to monitor the connection using connection_aborted () .

'connection_aborted ()' seems to respond to canceling the download or closing my browser window. My problem is that it does not respond with 100% accuracy. It registers about 50% of canceled downloads or closed browsers. If a connection is not interrupted, the download simply continues on the server, as if the browser had not canceled it.

Could you check 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); 

I am using the following:

  • Apache 2.4.4
  • PHP 5.4.12
  • MySQL 5.6.12
  • Google Chrome Version 32.0.1700.107 m
  • Windows 7 x64

Thanks.

0
php apache apache2 download
source share
1 answer

Add connection_status function and then try again

 if(connection_status()!=0||connection_aborted()!=0||){ //your code } 
0
source share

All Articles