What is the best possible way to detect if a download is completed, because afterward I want to update the database.
I tried some of this code from the PHP manual, but it doesn't do much for me:
header("Content-Type: application/octet-stream");
header("Content-Length: ".filesize($file));
header("Content-Disposition: attachment; filename=$filename");
// buffered read not using readfile($file);
if ($fp = fopen($bestand, 'rb')) {
while (!feof($fp)) {
$buf = fread($fp, 4096);
echo $buf;
$bytesSent += strlen($buf); /* We know how many bytes were sent to the user */
}
}
if ($bytesSent == filesize($fp)) {
//do something with db
}
That's not really going to tell you if the download has completed for the user. It will tell you when you have finished sending bytes to the user, but it says nothing about how many of them the user has actually received.
In reality, there is no way with PHP (which is a server-side, not a client-side, language) to truly detect when a file download has completed. The best you can do is log the download in your database when it begins. If you absolutely, completely and totally need to know when the download has completed, you'll have to do something like embed a Java applet or use Flash. However, usually that is not the correct answer in terms of usability for your user (why require them to have Java or Flash installed just to download something from you?).