当前位置: 首页 > 图文教程 > 网络编程 > PHP > 文件下载统计php编程

PHP
PHP新手总结的PHP基础知识
php实现gb2312和unicode间编码转换
用php语言实现数据库连接详细代码介绍
详细解析 PHP 向 MySQL 发送数据过程
利用PHP V5开发多任务应用程序
详细讲解PHP中缓存技术的应用
php escapeshellcmd多字节编码漏洞
《PHP设计模式介绍》导言
《PHP设计模式介绍》第一章 编程惯用法
《PHP设计模式介绍》第二章 值对象模式
《PHP设计模式介绍》第三章 工厂模式
《PHP设计模式介绍》第四章 单件模式
《PHP设计模式介绍》第五章 注册模式
《PHP设计模式介绍》第六章 伪对象模式
《PHP设计模式介绍》第七章 策略模式
《PHP设计模式介绍》第八章 迭代器模式
《PHP设计模式介绍》第九章 观测模式
《PHP设计模式介绍》第十章 规范模式
《PHP设计模式介绍》第十一章 代理模式
《PHP设计模式介绍》第十二章 装饰器模式

PHP 中的 文件下载统计php编程


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 262 ::
收藏到网摘: n/a

现在有许多站点下载文件都提供了统计功能,本文讨论的是如何使用php实现此功能,对于想隐藏下载文件路径,避免用户直接使用url下载的编程者,本文也具有一定的参考价值。
实现环境:linux apache php mysql

windows98 pws4 php mysql

一、数据库结构

数据库中创建一个表,存储文件信息,包括文件编码、名称、下载路径、统计,相应的sql文件内容如下:

create database 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);

二、php编程

1、 函数文件

函数文件包括数据库连接初始化函数和提示信息显示函数。

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>\";

}

?>



2、 下载连接页面

下载连接页面从数据库读取下载文件信息并显示。

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 \"&nbsp;\";

echo \"(下载次数:$arr_dlfile[count])\";

echo \"<br>\";

}

mysql_close($dl_in);

?>

</html>

3、 下载页面

当文件存在时,下载页面转到要下载的文件,如果发生错误,则显示提示信息。

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);