2011-12-20 85 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);