2011-12-20 80 views
1

我的网站上,让游客使用此代码下载各种文件的工作:怎么算下载的文件

<input type="button" class="downloadedFile" value="Download File" onclick="window.location.href=\'documents/' . $docsRow['docFileName'] . '\'" /> 

$ docsRow [“docFileName”]为每个文件,这是路径从MySQL数据库中检索。

大局观,这些文件是在一个jQuery的手风琴,每个手风琴窗格与一个或多个文档类 - 像这样的:

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

我的客户希望能够指望的数量每个文档已被下载的次数每类。我知道我可以通过javascript检测按钮点击,并且就“每个类”部分而言,我可以使用jQuery来检测按钮在哪个类表中点击,但我不明白的是1)如何将这些信息导入到PHP/MySQL代码中记录到数据库中,以及2)我是否只能检测到按钮点击,或者我是否可以检测用户是否实际将文件下载到了他们的计算机(这些是PDF,DOC, XLS文件)。

感谢任何人的帮助!

回答

4

有2个选项来计算下载次数。一个是链接到一个php页面,记录每个命中并重定向到实际文件。喜欢的东西:

<? 
$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.com/thefile.doc'); 
    ?> 

在HTML链接可以是这样的:

<a href="getfile.php?class=X&doc=Y">download file</a> 

第二种方法,我个人比较喜欢,是记录与阿贾克斯的点击次数。当用户点击链接下载做这样的事情:

$('a.thelink').click(function() { 
    $.post ('getfile.php', { class: X, doc: Y }); 
}); 

这样您发送要记录的数据,用户停留在同一页面。

如果你需要更多的清理让我知道。

+0

呃!阿贾克斯!不像我已经在网站的其他部分大量使用它! (我在6个月的停顿后重新认识自己)感谢Moshe(当然还有DaDaDom)! – marky 2011-12-20 15:20:15

+0

很高兴我能帮上忙 – 2011-12-20 15:23:19

3

对于第二部分:你不知道。如果用户请求了文档,他就可以得到它。 HTTP是无状态的,所以一旦点击,你只能尝试检查传输给用户的数量是否等于文档的大小......但这不是真的可能。

对于第一部分:不要直接链接到文档,而是链接到PHP页面,而不是将下载,尊重参数,并重定向到实际文档。或者它可以返回文档的二进制内容,无论你喜欢什么。

+0

到我听起来点。 – 2011-12-20 15:02:33

+0

感谢您的回复。我想尽办法检测他们是否真的下载了它。 - 就第一部分的回答而言,我并不清楚链接到计算下载的PHP页面的含义。你能给我一个更详细的解释吗?也许一些伪代码?谢谢! – marky 2011-12-20 15:10:11

+0

有关更详细的技术说明,请参阅Moshe Shahams的回答。 – 2011-12-20 15:11:51

0

另一个问题是直接通过记录每次下载的php文档来提供文档。

调用PHP文件为document-counter.php?file=path/file.ext

不要忘记urlencode($filename);

文档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);