I have this code here to allow the users to download files over my server:
$ch_2 = curl_init();
curl_setopt($ch_2, CURLOPT_USERAGENT, 'Mozilla/4.0');
curl_setopt($ch_2, CURLOPT_URL, $download_link);
curl_setopt($ch_2, CURLOPT_AUTOREFERER, true);
curl_setopt($ch_2, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch_2, CURLOPT_COOKIESESSION, false);
curl_setopt($ch_2, CURLOPT_FAILONERROR, true);
curl_setopt($ch_2, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch_2, CURLOPT_FORBID_REUSE, true);
curl_setopt($ch_2, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch_2, CURLOPT_NOSIGNAL, true);
curl_setopt($ch_2, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch_2, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch_2, CURLOPT_TIMEOUT, 86400);
curl_setopt($ch_2, CURLOPT_LOW_SPEED_LIMIT, 10240);
curl_setopt($ch_2, CURLOPT_LOW_SPEED_TIME, 60);
curl_setopt($ch_2, CURLOPT_MAX_RECV_SPEED_LARGE, 500);
curl_exec($ch_2);
$curl_errno_2 = curl_errno($ch_2);
curl_close($ch_2);
Theese files (linked in $download_link) are very big, around 1-3 GB.
This code works fine, but my problem is, when a user disconnects or abort the download then the script doesn’t stop until the server has received the full file ($download_link).
If I set “ignore_user_abort(true)”, then the script stops, but is it possible to check a client disconnection or download abort in the script and can handle this? For example, update a MySQL database entry or something else?
I know I can change this code by using “readfile()” or fopen, fread for example, but I will (and must) use this cURL code.
Is it possible to check this user disconnection or download abort, because I will only allow the user to make one download connection per IP address? So if they abort a download I can not update my MySQL database to mange the download connections form this IP address.
You can register a shutdown function. This will be called when the script finishes, when the user aborts or when you call exit() or die(). http://php.net/manual/en/function.register-shutdown-function.php
For example, (from
php.net):That’s the closest you’re going to get.
I would suggest you do this: