check if download is completed

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


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.