Jump to content

php + capture download from right-click Save Link As *SOLVED*


ricbax

Recommended Posts

Here is the situation:

I want to capture when a file gets downloaded, basically a counter that sends the value to my db.

The problem:

Due to plugins and browser issues I have disabled the the ability to download with a left-click (which rules out $_GET), forcing a right-click and the user choosing the Save Link As option from the pop-up menu.
So my question is how can I capture the right-click and progress a counter?

TIA

ricbax
Link to comment
Share on other sites

The only thing I can think of is to have your link point to a wrapper PHP file that updates your database, then sends a redirect header to the file you want to download.  Not sure if that works though. :)

BTW, just checked and it seems you can't attach a javascript event to the 'Save link as...' context menu item, only to the firing of the context menu itself.
Link to comment
Share on other sites

Erm. . . Something along the lines of this?

[code]
<?php


/*
* ... Increase your DB counter here ...
*/

// required for IE, otherwise Content-disposition is ignored
if(ini_get('zlib.output_compression'))
  ini_set('zlib.output_compression', 'Off');
 
// We'll be outputting a PDF
header('Content-type: application/pdf');

// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');

// The PDF source is in original.pdf
readfile('original.pdf');

?>
[/code]

I've lifted most of that from the [url=http://uk2.php.net/header]header[/url] manual page at PHP.net, and the IE bug workaround from somewhere else.  Not sure if that fix works though.  For much more on this sort of thing, do a 'Find on this page...' for 'Content-Disposition' on the above linked php.net header page.

good luck with it :)

EDIT: Of course, the above is just an example, you'd have to change MIME type for your specific file.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.