PHP+Mysql统计文件下载次数实例
程序员文章站
2023-08-30 09:42:35
PHP+Mysql统计文件下载次数实例,实现的原理也很简单,是通过前台点击链接download.php传参id,来更新点击次数。 ......
php+mysql统计文件下载次数实例,实现的原理也很简单,是通过前台点击链接download.php传参id,来更新点击次数。
获取文件列表:
<?php require 'conn.php'; $query = mysql_query("select * from downloads"); $lists = array(); while ($row = mysql_fetch_assoc($query)) { $lists[] = $row; } ?>
读取文件列表,并加上download.php链接和参数id:
<ul class="filelist"> <?php foreach ($lists as $v) { ?> <li><a href="download.php?id=<?php echo $v['id'] ?>"><?php echo $v['filename'] ?><span class="downcount" title="下载次数"><?php echo $v['downloads'] ?></span><span class="download">点击下载</span></a></li> <?php } ?> </ul>
点击下载按钮,累加文件次数:
$(function() { $('ul.filelist a').live('click', function() { var count = $('.downcount', this); count.text(parseint(count.text()) + 1); }); });
download.php文件源码:
<?php $id = (int) $_get['id']; if (!isset($id) || $id == 0) die('参数错误!'); $query = mysql_query("select * from downloads where id='$id'"); $row = mysql_fetch_array($query); if (!$row) exit; $filename = iconv('utf-8', 'gbk', $row['filename']); //中文名称注意转换编码 $savename = $row['savename']; //实际在服务器上的保存名称 $myfile = 'files/' . $savename; //文件 if (file_exists($myfile)) { mysql_query("update downloads set downloads=downloads+1 where id='$id'"); $file = @ fopen($myfile, "r"); header("content-type: application/octet-stream"); header("content-disposition: attachment; filename=" . $filename); while (!feof($file)) { echo fread($file, 50000); } fclose($file); exit; } else { echo '文件不存在!'; } ?>
本文转自: 转载请注明出处!
上一篇: 初识HTML_表单