代码实现下载计数。
已在数据库中创建一个表。
但是点击filelist.php3页面中的链接:
javascript:newopen('filedown.php3?id=000001'),
不是下载id=000001指向的地址,而是下载filedown.php3这段代码。
代码有以下几个函数组成:
1、建表,包括文件编码、名称、下载路径、统计
数据库: dl_db
CREATE TABLE dl_file (
id varchar(6),
name varchar(50),
url varchar(200),
count bigint(10)
);
INSERT INTO dl_file valueS( '000001', 'test', 'test.zip', 0);
INSERT INTO dl_file valueS( '000002', 'tif', 'download/123.tif', 0);
2、dl_func.php3 数据库连接初始化
<?
function dl_dbconnect(){
error_reporting(1+4); //禁掉warning性错误
$dl_in=0;
$dl_in=mysql_connect("localhost:3306","root","123456"
;
if(!dl_in) { //如果连接失败,退出
echo "数据库无法连接";
exit;
}
mysql_select_db("dl_db",$dl_in);
return $dl_in;
}
//显示提示信息的函数
function infopage($strInfo){
echo "<script language='javascript'>";
echo " window.alert('$strInfo');";
echo " history.back();";
echo "</script>";
}
?>
3、filelist.php3 下载连接页面
<html>
<head><title>文件下载</title>
<script language="javascript">
function newopen(url){
window.open(url,"_self"
;
return;
}
</script>
</head>
<?
require("dl_func.php3"
;
$dl_in=dl_dbconnect();
$strQuery="select * from dl_file order by id";
$dl_res=mysql_query($strQuery,$dl_in);
while($arr_dlfile=mysql_fetch_array($dl_res)){
echo "<a href=\"javascript:newopen('filedown.php3?id=$arr_dlfile[id]')\">";
echo "$arr_dlfile[name]";
echo " ";
echo "(下载次数:$arr_dlfile[count])";
echo "<br>";
}
mysql_close($dl_in);
?>
</html>
4、filedown.php3 下载页面
<?
require("dl_func.php3"
;
$dl_in=dl_dbconnect();
$strQuery="select url from dl_file where id='$id'";
$dl_res=mysql_query($strQuery,$dl_in);
if(!($arrfile=mysql_fetch_array($dl_res))){ //选择结果为空
infopage("错误的id号"
;
exit;
}else{
$arr_temp=split("/",$arrfile[url]);
$filename=$arr_temp[sizeof($arr_temp)-1];
if(strlen(trim($filename))==0){//文件名称为空
infopage("错误的文件"
;
exit;
}else{
$strQuery="update dl_file set count=count+1 where id='$id'";
mysql_query($strQuery,$dl_in);
header("Content-type: application/file"
;
header("Content-Disposition: attachment; filename=$filename"
;//缺省时文件保存对话框中的文件名称
header("location:$arrfile[url]"
;
//echo “this is test for echo-download”;
}
}
mysql_close($dl_in);
?>