Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if download is completed

Tags:

php

download

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
}
like image 291
Richard Avatar asked Oct 13 '09 22:10

Richard


People also ask

How can I tell if a file is fully downloaded?

You should create a checksum (an MD5 Sum, or SHA1 Sum) for the file on the server. Then after the download, run a the same checksum and the two values need to match. If you are downloading via Java, you can use the MessageDigest class to help you generate the digest.

How do I find an incomplete download file?

Whenever you encounter an incomplete/broken download, you would have to head back to the folder where the download files are stored. And, you need to find a file with an extension . CRDOWNLOAD (That's a chrome download file). It would normally start with the same name as you downloaded.

Why is my download not finishing?

Issues with Internet connectivity and the stability of the connection can cause downloads to fail, especially if the Internet connection is interrupted. When an Internet connection is unstable, it may disconnect and reconnect intermittently.


3 Answers

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?).

like image 171
Marc W Avatar answered Oct 19 '22 10:10

Marc W


As Marc W's already pointed out php can only tell you what you've sent from the server, not what the client's received, so using strictly php is a non-starter.

But there is always the possibility of asking the user to confirm the download by pressing a button that calls a JS check by generating an MD5 hash and comparing that to the MD5 hash of the file on your server (the on-server MD5 being generated by you), and on a successful comparison use that JS script to update your db or use it to call a php-script to do the same.

You could always just ask them to check the download is what they expected and, if they hit "yes, it's fine" then update the database.


Edited: in response to OP's comment.
Thanks, but you can't depend on the users cooperativeness. Because they also have to pay for the download, it is better that I have all the controll.I think I will then update the database when the script is accessed. That seems to be the most reliable then.

How about using an Ajax control to initiate -and maintain- the download, and, on completion verifying its integrity and running an update to your db?

I'd suggest not automatically assuming your customers are going to rip you off, though, whatever you end up choosing. Asking them nicely 'did it work for you?' and, if not, leading to a bug-report might be useful for you and them.

But, regardless, while some of your users might say 'no' just for the extra copy you have to balance the cost of that against the cost of your increased workload to prevent its happening. If the cost of maintaining 'honesty' exceeds the potential/likely cost of their taking an extra copy, then there's little point.

like image 26
David Thomas Avatar answered Oct 19 '22 12:10

David Thomas


I don't think it's possible via php to know how many bites have been sent out (and actually downloaded via the client), since there's no way to know if the user has cancelled the download or received the entire file.

like image 27
Ben Rowe Avatar answered Oct 19 '22 12:10

Ben Rowe