我想实现一个简单的计数器来跟踪文件被下载了多少次。我们这里讨论的是大文件,所以我不可能使用readfile(), fpassthru()或类似的方法将整个文件加载到php的内存中。
另外,对于这个解决方案,我想使用直接链接,然后通过jQuery.ajax更新计数器。我之所以选择这种方式,是因为带有X-Sendfile的download.php不适合我——我经常因为一次下载而收到对脚本的多个调用,这完全打乱了我的计数。(这可能是由于Chrome对图标的额外要求,但我不确定。而且,这不是问题。)
这就是我的index。html:
<a href="downloads/bla.zip"><span class="countDownload">bla</span></a>
这是jQuery:
$(document).ready(function() {
$("body").on("click", ".countDownload", function() {
var filename = $(this).parent().attr("href");
filename = filename.split("/");
filename = filename[filename.length - 1]
var request = $.ajax({
url: "counter.php?file=" + filename
});
request.done(function(msg) {
alert("yes");
});
request.fail(function(jqXHR, textStatus) {
alert("no");
});
// if this is here, ajax works, but download fails
return false;
});
});
如果有"return false;",则ajax请求将成功,但文件下载将被抑制。如果"return false;"不存在,则ajax请求将失败("取消"),但反过来文件下载工作正常。
任何帮助都是感激的!
100%未经测试,但只是一个想法…
$(document).ready(function() {
$("body").on("click", ".countDownload", function(e) {
e.preventDefault();
var filename = $(this).parent().attr("href");
filename = filename.split("/");
filename = filename[filename.length - 1]
var request = $.ajax({
url: "counter.php?file=" + filename
});
request.done(function(msg) {
alert("yes");
window.location.href = "*****URL******"+filename;
});
request.fail(function(jqXHR, textStatus) {
alert("no");
});
// if this is here, ajax works, but download fails
return false;
});
});
显然填写你自己的URL。
这个呢
<a href="downloads/bla.zip" id="download" style="display:none"></a>
<span class="countDownload">bla</span>
$(document).ready(function() {
$("body").on("click", ".countDownload", function() {
var filename = $("#download").attr("href");
filename = filename.split("/");
filename = filename[filename.length - 1]
var request = $.ajax({
url: "counter.php?file=" + filename
});
request.done(function(msg) {
alert("yes");
});
request.fail(function(jqXHR, textStatus) {
alert("no");
});
$("#download").trigger("click");
return false;
});
});
首先它会通过你的ajax
请求然后触发anchor
的点击事件
你的HTML:
<div class="result"></div>
<button url="downloads/bla.zip">Click to download the file</button>
脚本:
$(document).ready(function()
{
$('button').on('click',function()
{
var filename = $(this).attr('url');
var request = $.ajax(
{
url: "download_file.php?file=" + filename
});
request.done(function(msg)
{
$('.result').html('Thank you for downloading!');
});
request.fail(function(jqXHR, textStatus)
{
$('.result').html('Download failed!');
});
});
});
download_file.php ?文件=下载/bla.zip:
//First find this file :)
if(!empty($_POST['file']))
{
$dirpath = 'location/of/file/';
if(file_exists($dirpath))
{
$file = $dirpath.$_POST['file'];
header('Content-Description: Secure file download');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
//DO YOUR COUNTING SOMEWHERE HERE:
exit;
}
}
else
{
//LOG ERROR HERE:
exit;
}
为了保护你的文件,把这个。htaccess放到你的下载文件夹中:
Order deny,allow
Deny from all
我没有测试上述内容。希望你能从这里自己弄清楚。祝你好运