Flag a node after click on the link I want to check if user has downloaded the file so afterwards I will allow him to post a review. To do that I want to flag the node after user download file, flag the node and allow flagged node to be commented. I have a script:

global $user;
$flag = flag_get_flag('downloaded');
$node = node_load(arg(1));
$flag-&gt;flag('downloaded', $node-&gt;nid, user_load($user-&gt;uid), TRUE);


but I don't know how to make that event to occure when clicked on link. Link is in span.file a

Does anybody have an idea how to achieve my goal? I will be so grateful for any answer.
If you're using the private file system, you can make use of hook_file_download, it is normally used for granting access but I don't see why not you can use it for flagging stuff.

See an example of this in Example of hook_file_download

If you're using the public one, it will be way trickier, you're going to need to wrap the download link to react with your custom code, this could be done by replacing the download link like the Public Download Count module does, or providing your own hook_menu callback that is called via ajax to flag the content, but then you depend on whether the user has js enabled or not.