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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.