Say a user clicks on a link to download a file. The user gets the save as dialogue box but then clicks on cancel. How do you detect this? Meaning if the user hits the cancel link or doesn't get the whole file, the server should not record that the file was downloaded.
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
When a server sends a file, it doesn't just stream over the wire instantly, there are ack packets that have to go between client and server sending the file out one chunk at a time.
All one has to do is have some sort of process that sends the file to the user in a managed way, so that you can hook an event on wherever the "last" block of the file is flushed to the user.
Now granted, the user might never recieve this last block, but if they ask for the last block ( which is what ack packets are doing ) then either they, or the underlying network protocol has accepted that they've received all the blocks preceding that last block, and that one may proceed to send the last block.
Now assuming that
All one needs is a simple construct like this: ( Pseudo Code ).
Edit Unkwntech's answer is effectively the same as my own.
His answer is however PHP specific and I wanted to provide a more language generic solution, primarily, because I've developed a distaste for PHP, and am loath to need to write code for it and set up a web-server and test it works just to prove a point that I know from experience works.
Apologies for being too lazy in this regard.
In the past I have done this:
log should only run after readfile is complete meaning the entire file has been streamed to the user. It's been a while since I have done this and I don't know where the exact code is so this may not be exact but it should get you going.
EDIT I found the above code on php.net and it is more or less what I was describing, this method should work.
one way would be to write some PHP that handles actually delivering the file in question supplied as a $_REQUEST parameter. That script would do the actual work of logging the download
Are you talking about any file? If it's a program you can have it make a call to the server on install? Other than that I agree with Jonathan, not really sure you have access to that.
I think some HTTPd's don't log files into the access log until the request is rendered complete. You could try writing something that parses the log, greps the filename, and counts the number of lines.
Edit: Just tried with nginx. Confirmed my theory. I believe the same is true for Apache.
Edit #2: Why was this modded down? It's actually the correct answer according to the dupe-link on SO. I stand vindicated.