PHP+Mysql统计文件下载次数实例
PHP+Mysql统计文件下载次数实例,统计实现的文件原理也很简单,是亿华云计算下载通过前台点击链接download.php传参id,来更新点击次数。次数
获取文件列表:
1 2 3 4 5 6 7 8 9 <?实例php require conn.php; $query = mysql_query("SELECT * FROM downloads"); $lists = array(); while ($row = mysql_fetch_assoc($query)) { $lists[] = $row; } ?>读取文件列表,源码下载并加上download.php链接和参数id:
1 2 3 4 5 <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>点击下载按钮,累加文件次数:
1 2 3 4 5 6 $(function() { $(ul.filelist a).live(click,文件 function() { var count = $(.downcount, this); count.text(parseInt(count.text()) + 1); }); });download.php文件源码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 <?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 文件不存在!云服务器提供商; } ?下载>本文转自: https://www.sucaihuo.com/php/224.html 转载请注明出处!