linux社区爱心援助Linux认证系列教程业界动态站务新闻公司招聘网络学院网址大全LPI专题CISCO专题
设为首页
加入收藏
管理团队
JSP  
JAVA  
PERL  
 您的位置:首页 > 开发语言 > php >
栏目导栏
  php
  JSP
  ASP
  asp.net
  JAVA
  c/c++/c#
  perl
  JavaScript
  Basic
  Delphi
资料搜索
热门文章
·PHP生成静态页面的一些经验
·PHP无限分类与树型论坛的实现方
·php5学习笔记
·入门级PHP程序员面试题
·PHP 和 MySQL初学入门
·PHP入门速成
·vim的代码折叠
·文件下载统计php编程代码
·如何建立自己的新闻发布系统
·织梦CMS中文转换拼音函数研究
·Windows下Apache+Tomcat+MySQL
·PHP初学者头疼问题总结
·Cookie及其使用详细介绍
·生成sessionid和随机密码的例子
·使用无限生命期Session的方法
最新文章
·PHP入门速成
·用php实现广告轮播
·Zend Optimizer 问题浅析
·功能强大的CGI语言----PHP3
·用Session对Web页面进行保护
·PHP--进行模块化设计
·如何将PHP的结果输出到非PHP页
·如何开发一个虚拟域名系统
·PHP4调用自己编写的COM组件
·简单的页面缓冲技术(三)
·简单的页面缓冲技术(二)
·简单的页面缓冲技术(一)
·用Socket发送电子邮件(二)
·用Socket发送电子邮件(一)
·PHP/MySQL 购物车
Google
 
文件下载统计php编程代码
[ 作者:  加入时间:2007-10-11 12:42:23  来自:Linux联盟收集整理 ]

现在有许多站点下载文件都提供了统计功能,本文讨论的是如何使用php实现此功能,对于想隐藏下载文件路径,避免用户直接使用url下载的编程者,本文也具有一定的参考价值。 Pn7Linux联盟
实现环境:linux apache php mysqlPn7Linux联盟
Pn7Linux联盟
windows98 pws4 php mysqlPn7Linux联盟
Pn7Linux联盟
一、数据库结构Pn7Linux联盟
Pn7Linux联盟
数据库中创建一个表,存储文件信息,包括文件编码、名称、下载路径、统计,相应的sql文件内容如下:Pn7Linux联盟
Pn7Linux联盟
create database dl_db;Pn7Linux联盟
Pn7Linux联盟
create table dl_file (Pn7Linux联盟
Pn7Linux联盟
id varchar(6),Pn7Linux联盟
Pn7Linux联盟
name varchar(50),Pn7Linux联盟
Pn7Linux联盟
url varchar(200),Pn7Linux联盟
Pn7Linux联盟
count bigint(10)Pn7Linux联盟
Pn7Linux联盟
);Pn7Linux联盟
Pn7Linux联盟
insert into dl_file values( \'000001\', \'test\', \'test.zip\', 0);Pn7Linux联盟
Pn7Linux联盟
insert into dl_file values( \'000002\', \'tif\', \'download/123.tif\', 0);Pn7Linux联盟
Pn7Linux联盟
二、php编程Pn7Linux联盟
Pn7Linux联盟
1、 函数文件Pn7Linux联盟
Pn7Linux联盟
函数文件包括数据库连接初始化函数和提示信息显示函数。Pn7Linux联盟
Pn7Linux联盟
dl_func.php3:Pn7Linux联盟
Pn7Linux联盟
<?Pn7Linux联盟
Pn7Linux联盟
//初始化数据库连接的程序Pn7Linux联盟
Pn7Linux联盟
function dl_dbconnect(){Pn7Linux联盟
Pn7Linux联盟
error_reporting(1 4); //禁掉warning性错误Pn7Linux联盟
Pn7Linux联盟
$dl_in=0;Pn7Linux联盟
Pn7Linux联盟
$dl_in=mysql_connect(\"localhost:3306\",\"root\",\"123456\");Pn7Linux联盟
Pn7Linux联盟
if(!dl_in) { //如果连接失败,退出Pn7Linux联盟
Pn7Linux联盟
echo \"数据库无法连接\";Pn7Linux联盟
Pn7Linux联盟
exit;Pn7Linux联盟
Pn7Linux联盟
}Pn7Linux联盟
Pn7Linux联盟
mysql_select_db(\"dl_db\",$dl_in);Pn7Linux联盟
Pn7Linux联盟
return $dl_in;Pn7Linux联盟
Pn7Linux联盟
}Pn7Linux联盟
Pn7Linux联盟
Pn7Linux联盟
Pn7Linux联盟
//显示提示信息的函数Pn7Linux联盟
Pn7Linux联盟
function infopage($strinfo){Pn7Linux联盟
Pn7Linux联盟
echo \"<script language=\'javascript\'>\";Pn7Linux联盟
Pn7Linux联盟
echo \" window.alert(\'$strinfo\');\";Pn7Linux联盟
Pn7Linux联盟
echo \" history.back();\";Pn7Linux联盟
Pn7Linux联盟
echo \"</script>\";Pn7Linux联盟
Pn7Linux联盟
}Pn7Linux联盟
Pn7Linux联盟
?>Pn7Linux联盟
Pn7Linux联盟
Pn7Linux联盟
Pn7Linux联盟
2、 下载连接页面Pn7Linux联盟
Pn7Linux联盟
下载连接页面从数据库读取下载文件信息并显示。Pn7Linux联盟
Pn7Linux联盟
filelist.php3:Pn7Linux联盟
Pn7Linux联盟
<html>Pn7Linux联盟
Pn7Linux联盟
<head><title>文件下载</title>Pn7Linux联盟
Pn7Linux联盟
<script language=\"javascript\">Pn7Linux联盟
Pn7Linux联盟
function newopen(url){Pn7Linux联盟
Pn7Linux联盟
window.open(url,\"_self\");Pn7Linux联盟
Pn7Linux联盟
return;Pn7Linux联盟
Pn7Linux联盟
}Pn7Linux联盟
Pn7Linux联盟
</script>Pn7Linux联盟
Pn7Linux联盟
</head>Pn7Linux联盟
Pn7Linux联盟
<?Pn7Linux联盟
Pn7Linux联盟
require(\"dl_func.php3\");Pn7Linux联盟
Pn7Linux联盟
$dl_in=dl_dbconnect();Pn7Linux联盟
Pn7Linux联盟
$strquery=\"select * from dl_file order by id\";Pn7Linux联盟
Pn7Linux联盟
$dl_res=mysql_query($strquery,$dl_in);Pn7Linux联盟
Pn7Linux联盟
while($arr_dlfile=mysql_fetch_array($dl_res)){Pn7Linux联盟
Pn7Linux联盟
echo \"<a href=\\"javascript:newopen(\'filedown.php3?id=$arr_dlfile[id]\')\\">\";Pn7Linux联盟
Pn7Linux联盟
echo \"$arr_dlfile[name]\";Pn7Linux联盟
Pn7Linux联盟
echo \"&nbsp;\";Pn7Linux联盟
Pn7Linux联盟
echo \"(下载次数:$arr_dlfile[count])\";Pn7Linux联盟
Pn7Linux联盟
echo \"<br>\";Pn7Linux联盟
Pn7Linux联盟
}Pn7Linux联盟
Pn7Linux联盟
mysql_close($dl_in);Pn7Linux联盟
Pn7Linux联盟
?>Pn7Linux联盟
Pn7Linux联盟
</html>Pn7Linux联盟
Pn7Linux联盟
3、 下载页面Pn7Linux联盟
Pn7Linux联盟
当文件存在时,下载页面转到要下载的文件,如果发生错误,则显示提示信息。Pn7Linux联盟
Pn7Linux联盟
filedown.php3:Pn7Linux联盟
Pn7Linux联盟
<?Pn7Linux联盟
Pn7Linux联盟
require(\"dl_func.php3\");Pn7Linux联盟
Pn7Linux联盟
$dl_in=dl_dbconnect();Pn7Linux联盟
Pn7Linux联盟
$strquery=\"select url from dl_file where id=\'$id\'\";Pn7Linux联盟
Pn7Linux联盟
$dl_res=mysql_query($strquery,$dl_in);Pn7Linux联盟
Pn7Linux联盟
if(!($arrfile=mysql_fetch_array($dl_res))){ //选择结果为空Pn7Linux联盟
Pn7Linux联盟
infopage(\"错误的id号\");Pn7Linux联盟
Pn7Linux联盟
exit;Pn7Linux联盟
Pn7Linux联盟
}else{Pn7Linux联盟
Pn7Linux联盟
$arr_temp=split(\"/\",$arrfile[url]);Pn7Linux联盟
Pn7Linux联盟
$filename=$arr_temp[sizeof($arr_temp)-1];Pn7Linux联盟
Pn7Linux联盟
if(strlen(trim($filename))==0){//文件名称为空Pn7Linux联盟
Pn7Linux联盟
infopage(\"错误的文件\");Pn7Linux联盟
Pn7Linux联盟
exit;Pn7Linux联盟
Pn7Linux联盟
}else{Pn7Linux联盟
Pn7Linux联盟
$strquery=\"update dl_file set count=count 1 where id=\'$id\'\";Pn7Linux联盟
Pn7Linux联盟
mysql_query($strquery,$dl_in);Pn7Linux联盟
Pn7Linux联盟
header(\"content-type: application/file\");Pn7Linux联盟
Pn7Linux联盟
header(\"content-disposition: attachment; filename=$filename\");//缺省时文件保存对话框中的文件名称Pn7Linux联盟
Pn7Linux联盟
header(\"location:$arrfile[url]\");Pn7Linux联盟
Pn7Linux联盟
//echo “this is test for echo-download”;Pn7Linux联盟
Pn7Linux联盟
}Pn7Linux联盟
Pn7Linux联盟
}Pn7Linux联盟
Pn7Linux联盟
mysql_close($dl_in);Pn7Linux联盟
Pn7Linux联盟
?>Pn7Linux联盟
Pn7Linux联盟
实现的原理是filelist.php3显示所有文件的连接,然后根据传递的id来得到文件的名称和路径,通过重新定位来下载文件。以上程序笔者测试过,运行正常。Pn7Linux联盟
Pn7Linux联盟
文件url可以是本地的,也可以是其他服务器上的。Pn7Linux联盟
Pn7Linux联盟
如果文件内容存储在数据库中,或者文件没有在http和ftp的路径下,解决的方法可以利用将文件的内容echo出来取代header(“location:$arrfile[url]”),由于读取文件方法相对简单,这里不再赘述。

Linux联盟收集整理 ,转贴请标明原始链接,如有任何疑问欢迎来本站Linux论坛讨论
评论】【加入收藏夹】【 】【打印】【关闭
※ 相关链接
 ·使代码简洁的五条忠告  (2007-10-30 15:16:21)
 ·delphi IDE的代码完成功能  (2007-10-30 15:06:47)
 ·Javascript制作声音按钮的代码  (2007-10-30 13:53:06)
 ·如何将asp.net的后台cs代码移动到页面上  (2007-10-29 14:25:45)
 ·IE浏览器右下脚弹出的广告特效代码  (2007-10-29 13:52:47)
 ·详细解析网页漂浮广告代码的含义  (2007-10-29 13:49:01)
 ·常用的网页特殊效果JS代码  (2007-10-29 13:48:25)
 ·限制文本字节数的JS源代码程序  (2007-10-29 13:47:48)
 ·让你的ewbeditor也能运行代码  (2007-10-29 13:33:35)
 ·实现类似于Flash的超酷特效代码  (2007-10-29 13:28:52)