Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You will want to check out the following functions: connection_status(), connection_aborted() and ignore_user_abort() (see the <a href="http://www.php.net/manual/en/features.connection-handling.php" rel="nofollow">connection handling</a> section of the PHP manual for more info).</p> <p>Although I can't guarantee the reliability (it's been a while since I've played around with it), with the right combination you should be able to accomplish what you want. There are a few caveats when working with these though, the big one being that if something goes wrong you could end up with stranded PHP scripts running on the server requiring you to kill Apache to stop them.</p> <p>The following should give you a good idea of how to do it (adapted from the PHP code examples and a couple of the comments):</p> <pre><code>&lt;?php //Set PHP not to cancel execution if the connection is aborted //and drop the time limit to allow for big file downloads ignore_user_abort(true); set_time_limit(0); while(true){ //See the ignore_user_abort() docs re having to send data echo chr(0); //Make sure the data gets flushed properly or the connection check won't work flush(); ob_flush(); //Check then connection status and exit loop if aborted if(connection_status() != CONNECTION_NORMAL || connection_aborted()) break; //Just to provide some spacing in this example sleep(1); } file_put_contents("abort.txt", "aborted\n", FILE_APPEND); //Never hurts to ensure that the script halts execution die(); </code></pre> <p>Obviously for how you would be using it the data being sent would simply be the download data chunk (just make sure you flush the buffer properly to ensure the data is actually sent). As far as I'm aware, there is no way of making a distinction between pausing and aborting/stopping. Pause/resume functionality (and multi-part downloading - i.e. how download managers accelerate downloads) relies on the "Range" header, basically requesting byte x to byte y of the file. So if you want to allow resumable downloads you'll have to deal with that too.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload