php功能代码下载,PHP实现下载功能的代码

PHP实现下载功能的代码,并实现安全下载,隐藏文件真实地址等,需要的朋友可以参考下

wzskynet#163.com

·php escapeshellcmd多字节编码漏洞

·详细讲解PHP中缓存技术的应用

·利用PHP V5开发多任务应用程序

·详细解析 PHP 向 MySQL 发送数据过程

·PHP实现静态发布的方法浅谈

你一定会笑我“下载文件”如此简单都值得说?当然并不是想你想象的那么简单。例如你希望客户要填完一份表格,才可以下载某一文件,你第一个想法一定是用 “Redirect”的方法,先检查表格是否已经填写完毕和完整,然后就将网址指到该文件,这样客户才能下载,例如笔者编写的以下代码:

复制代码 代码如下:

// 检查 FORM 是否全部填写完毕…

if ($form_completed) {

Header(“Location: http://www.jb51.net/download/info_check.exe”);

exit;

}

?>

或者是以下的情况:

复制代码 代码如下:

开始下载文件

这里利用了ID方式接收要下载文件的编号,然后用“Redirect”的方式连接到实际的网址。

如果你想做一个关于“网上购物”的电子商务网站,考虑安全问题,你不想用户直接复制网址下载该文件,笔者建议你使用PHP直接读取该实际文件然后下载的方法去做。程序如下:

复制代码 代码如下:

$file_name = “info_check.exe”;

$file_dir = “/public/www/download/”;

if (!file_exists($file_dir . $file_name)) { //检查文件是否存在

echo “文件找不到”;

exit;

} else {

$file = fopen($file_dir . $file_name,”r”); // 打开文件

// 输入文件标签

Header(“Content-type: application/octet-stream”);

Header(“Accept-Ranges: bytes”);

Header(“Accept-Length: “.filesize($file_dir . $file_name));

Header(“Content-Disposition: attachment; filename=” . $file_name);

// 输出文件内容

echo fread($file,filesize($file_dir . $file_name));

fclose($file);

exit;}

?>

而如果文件路径是“http”或者“ftp” 网址的话,则源代码会有少许改变,程序如下:

复制代码 代码如下:

<?

$file_name = “info_check.exe”;

$file_dir = “http://www.jb51.net/”;

$file = @ fopen($file_dir . $file_name,”r”);

if (!$file) {

echo “文件找不到”;

} else {

Header(“Content-type: application/octet-stream”);

Header(“Content-Disposition: attachment; filename=” . $file_name);

while (!feof ($file)) {

echo fread($file,50000);

}

fclose ($file);

}

?>

这样就可以用PHP直接输出文件了。

实现php文件安全下载!

复制代码 代码如下:

public function downloads($name){

$name_tmp = explode(“_”,$name);

$type = $name_tmp[0];

$file_time = explode(“.”,$name_tmp[3]);

$file_time = $file_time[0];

$file_date = date(“Y/md”,$file_time);

$file_dir = SITE_PATH.”/data/uploads/$type/$file_date/”;

if (!file_exists($file_dir.$name)){

header(“Content-type: text/html; charset=utf-8”);

echo “File not found!”;

exit;

} else {

$file = fopen($file_dir.$name,”r”);

Header(“Content-type: application/octet-stream”);

Header(“Accept-Ranges: bytes”);

Header(“Accept-Length: “.filesize($file_dir . $name));

Header(“Content-Disposition: attachment; filename=”.$name);

echo fread($file, filesize($file_dir.$name));

fclose($file);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PHP实例之新闻发布系统 Create TABLE `news` ( `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY , `p_time` DATETIME NOT NULL , `title` VARCHAR( 80 ) NOT NULL , `detail` TEXT NOT NULL ) TYPE = innodb; create table news(id int not null auto_increment primary key, p_time datetime not null, title varchar(80)not null, detail text not null )type=innodb; 数据库连接:conn.php <? $conn = @mysql_connect('localhost','root','')or die(mysql_error()."不能连接到数据库!"); //连接数据库; $db = mysql_select_db('news',$conn); $page_size = 8; //每页最多显示新闻条数; ?> 添加新闻页面:new.php <? $title="新闻发布系统"; include("inc/header.inc");//头文件 ?> <style type="text/css"> <!-- .STYLE1 {font-size: 12px} .title { font-size: 12px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; font-family: "宋体"; color: #993300; line-height: normal; height: 16px; } .field { font-family: "宋体"; font-size: 12px; color: #993333; } .STYLE2 { font-size: 16px; font-weight: bold; } --> </style> <p align="center" class="STYLE2">PHP+Mysql新闻发布</p> <form action="post.php" method="post" name="frm" id="frm"> <table width="500" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#0066FF"> <tr> <td height="1" bgcolor="#FFFFFF"><span class="STYLE1">标题:</span></td> </tr> <tr> <td height="1" bgcolor="#FFFFFF"><input name="title" type="text" class="title" id="title" size="60" maxlength="80"></td> </tr> <tr> <td height="1" bgcolor="#FFFFFF"><span class="STYLE1">内容: </span></td> </tr> <tr> <td height="1" bgcolor="#FFFFFF"><textarea name="textfield" cols="58" rows="6" class="field"></textarea></td> </tr> <tr> <td bgcolor="#FFFFFF"><input name="submit" type="submit" value="发布"></td> </tr> </table> </form> <? include("inc/navbar.inc");//底部 ?> 新闻处理页面:post.php <? include"conn.php"; $title=htmlspecialchars($_POST['title']); $textfield=htmlspecialchars($_POST['textfield']); $pub_time=date('Y')."-".date('m')."-".date('d')." ".date('H').":".date('i').":".date('s'); $query="insert into news(title,detail,p_time)values ('$title','$textfield','$pub_time')"; $result=mysql_query($query); if($result) { echo "发布成功!<br>"; echo "<a href='list.php'>新闻列表</a> <a href='new.php'>继续发布</a>"; } else { echo mysql_error()."<br>"; echo "发布失败!请<a >返回</a>"; } ?> 新闻列表:list.php <? include "conn.php"; $query = "Select COUNT(*) FROM news"; $result = mysql_query($query); $num = mysql_num_rows($result); $page_count = ceil($num/$page_size); //$offset = ($page_count-1)*$page_size; if(empty($_GET['page'])) { $page = 1; }else { $page = $_GET['page']; if($page<=0) { $page = 1; }else { $page >= $page_count; $page = $page_count-1; } } $query ="Select * FROM `news` orDER BY `id` DESC LIMIT ".($page-1)*$page_count.","."$page_size"; $result = mysql_query($query); ?> <table width="571" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#0066FF"> <tr> <td width="5" bgcolor="#FFFFFF"><div align="center"></div></td> <td width="343" bgcolor="#FFFFFF"><div align="center">标题</div></td> <td width="189" bgcolor="#FFFFFF"><div align="center">时间</div></td> </tr> <?php while($l_result = mysql_fetch_array($result)) { ?> <tr> <td bgcolor="#FFFFFF"><div align="center"><?php echo $l_result['id'];?></div> </td> <td bgcolor="#FFFFFF"><div align="center"><a href="<?php echo "><?php echo $l_result['title'];?></a></div></td> <td bgcolor="#FFFFFF"><div align="center"><a href="<?php echo "><?php echo $l_result['p_time'];?></a></div></td> </tr> <?php } ?> </table><br> <center><a href="new.php">发布信息</a><br></center> <?php //页码显示 for ($i=1;$i<=($page_count-1);$i++){ echo "<a page=".$i.">".$i."</a> "; } //页码显示 ?> 新闻显示页面:view.php <? include "conn.php"; $query = "Select * FROM news where id=".$_GET['id']; $result = mysql_query($query); $v_result = @mysql_fetch_array($result); ?> <table width="60%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#0066FF"> <tr> <td bgcolor="#FFFFFF">标题:</td> </tr> <tr> <td bgcolor="#FFFFFF"><?php echo $v_result['title'];?></td> </tr> <tr> <td bgcolor="#FFFFFF">内容:</td> </tr> <tr> <td bgcolor="#FFFFFF"><?php echo $v_result['detail'];?></td> </tr> <tr> <td bgcolor="#FFFFFF"><a href="list.php">返回</a></td> </tr> </table>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值