I am working on a site that allows visitors to download various documents using this code:
<input type="button" class="downloadedFile" value="Download File" onclick="window.location.href=\'documents/' . $docsRow['docFileName'] . '\'" />
$docsRow['docFileName'] is the path of each document, which is retrieved from a MySQL database.
The bigger picture is that these documents are in a jQuery accordion, with each accordion pane being a class with one or more documents - like such:
<div id="accordion">
<h3><a href="#">LEAD5220 - Leader as Change Agent</a></h3>
<div>
<p class="className">Leader as Change Agent</p>
<p class="classNumber">LEAD5220</p>
<hr class="classSeparator" />
<table width="95%">
<tr>
<td><strong>Document Name:</strong> Generic Annotation Format</td>
<td width="100px" align="right"><input type="button" class="downloadedFile" value="Download File" onclick="window.location.href='documents/Generic Annotation Format.docx'" /></td>
</tr>
<tr>
<td colspan="2"><strong>Description:</strong> This document can be used as a template for your annotations in the annotated bibliography forums.</td>
</tr>
</table>
<hr class="classSeparator" />
...
My client wants to be able to count the number of times each document has been downloaded per class.
I known I can detect the button click via javascript, and as far as the "per class" part, I can use jQuery to detect in which class table the button clicked, but what I don't understand is 1) how to get that info into PHP/MySQL code to record to the database, and 2) whether I can only detect the button click, or whether I can detect if the user actually downloaded the file to their puter (these are PDF, DOC, XLS files).
Thanks for anyone's help!
I am working on a site that allows visitors to download various documents using this code:
<input type="button" class="downloadedFile" value="Download File" onclick="window.location.href=\'documents/' . $docsRow['docFileName'] . '\'" />
$docsRow['docFileName'] is the path of each document, which is retrieved from a MySQL database.
The bigger picture is that these documents are in a jQuery accordion, with each accordion pane being a class with one or more documents - like such:
<div id="accordion">
<h3><a href="#">LEAD5220 - Leader as Change Agent</a></h3>
<div>
<p class="className">Leader as Change Agent</p>
<p class="classNumber">LEAD5220</p>
<hr class="classSeparator" />
<table width="95%">
<tr>
<td><strong>Document Name:</strong> Generic Annotation Format</td>
<td width="100px" align="right"><input type="button" class="downloadedFile" value="Download File" onclick="window.location.href='documents/Generic Annotation Format.docx'" /></td>
</tr>
<tr>
<td colspan="2"><strong>Description:</strong> This document can be used as a template for your annotations in the annotated bibliography forums.</td>
</tr>
</table>
<hr class="classSeparator" />
...
My client wants to be able to count the number of times each document has been downloaded per class.
I known I can detect the button click via javascript, and as far as the "per class" part, I can use jQuery to detect in which class table the button clicked, but what I don't understand is 1) how to get that info into PHP/MySQL code to record to the database, and 2) whether I can only detect the button click, or whether I can detect if the user actually downloaded the file to their puter (these are PDF, DOC, XLS files).
Thanks for anyone's help!
Share Improve this question asked Dec 20, 2011 at 14:53 markymarky 5,08818 gold badges63 silver badges110 bronze badges3 Answers
Reset to default 4There are 2 options to count the number of downloads. One is to link to a php page that records every hit and than redirects to the actual file. Something like:
<?
$class = $_REQUEST['class'];
$doc = $_REQUEST['doc'];
// insert the code here to get and update the number of hits to this class/doc
// now redirect
header('Location: http://www.example./thefile.doc');
?>
In the html the link can be something like:
<a href="getfile.php?class=X&doc=Y">download file</a>
The second approach, which I personally prefer, is to record the number of hits with ajax. When the user clicks the link to download do something like:
$('a.thelink').click( function() {
$.post ('getfile.php', { class: X, doc: Y });
});
this way you send the data to be recorded, and the user stays in the same page.
If you need more clearifications let me know.
For the second part: You don't know. If the user requested the document, he got it. HTTP is stateless, so once clicked you could only try to check if the amount transferred to the user equals the size of the document ... But that's not really possible.
For the first part: Don't link to the document directly but to a PHP page instead which counts the download, respecting the parameter, and redirects to the actual document. Or it may return the binary content of the document, whatever you like.
Another aproach is to serve the document directly through the php document that records each download.
you call the php file as document-counter.php?file=path/file.ext
don't forget to urlencode($filename);
document-counter.php
<?php
if ( ! isset($_GET['file'])) {
die ('_GET file not set');
}
$file = realpath($_SERVER['DOCUMENT_ROOT'] . urldecode($_GET['file']));
if ( ! file_exists($file)) {
die($file . ' not exists');
}
$size = filesize($imageFile);
$extension = pathinfo ($imageFile, PATHINFO_EXTENSION);
header('HTTP/1.1 200 OK');
header('Pragma: no-cache');
// Attention: the following line can vary depending of the extension of the file !
header('Content-Type: application/' . $extension);
header('Content-Length: ' . $size);
// here, ***after the header***, goes all the stuff for incrementing the counters and
// saving them
readfile($imageFile);
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745467208a4628969.html
评论列表(0条)