Manoj ahirwar
Manoj ahirwar

Reputation: 1106

How can I track number of downloads for images in website

I have a <img download> tag in my page. Every time a user clicks on it, file will download.

I can track number of clicks on this <img> tag and store in db for track number of downloads.

But what if user cancel the download file prompt (Select location for downloading file),

How can I track whether user is actually downloading file or not?

Upvotes: 1

Views: 305

Answers (1)

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167172

You need to use a proxy for the file. Say something like this PHP file:

<?php
  $file = $_GET["file"];                     // Get the file.
  header("Content-disposition: attachment"); // And other headers for the file.
  echo file_get_contents($file);             // Start the file.
  // Update the counter.
  file_put_contents(intval(file_get_contents("count.txt")) + 1, "count.txt");
?>

On a good note, this // Update code executes only when the request is sent, and file is downloaded fully. Correct me if I am wrong.

If the request is sent and the code is executed, sorry, Google Chrome and Firefox starts the download of the file when the Save dialog box is there, which means, when the Save dialog box is open, the file has already started downloading and when cancel is clicked, the user doesn't get the file, but the server already receives the request, so it is like downloading a file and deleting it.

Note: Before some genius comes and says, this is vulnerable, file-name is missing, etc., let me tell you well ahead, this is just an idea to help the OP.

Upvotes: 2

Related Questions