服务报价 | 域名主机 | 网络营销 | 软件工具| [加入收藏]
 热线电话: #
当前位置: 主页 > php教程 > php教程 >

php下载文件源代码

时间:2016-02-25 08:30来源: 作者: 点击:
一个简单的php文件下载源代码,虽不支持断点续传等,但是可以满足一些常用的需求了。php下载文件其实用一个a标签就能实现,比如 a href=web/magento-1.8.1.0.zipmagento-1.8.1.0.zip/a 。但是遇到
一个简单的php文件下载源代码,虽不支持断点续传等,但是可以满足一些常用的需求了。php下载文件其实用一个a标签就能实现,比如 <a href="web/magento-1.8.1.0.zip">magento-1.8.1.0.zip</a> 。但是遇到一些浏览器能识别的格式,比如.txt,.html,.pdf等,再用<a href="web/abc.txt">abc.txt</a> 想必也知道会发生什么了。
 
 
 
 
 
<?php 
 
 
 
/**
 
 * 文件下载
 
 *
 
**/
 
 
 
header("Content-type:text/html;charset=utf-8");
 
 
 
download('web/magento-1.8.1.0.zip', 'magento下载');
 
 
 
function download($file, $down_name){
 
 
 
$suffix = substr($file,strrpos($file,'.')); //获取文件后缀
 
$down_name = $down_name.$suffix; //新文件名,就是下载后的名字
 
 
 
//判断给定的文件存在与否 
 
if(!file_exists($file)){
 
die("您要下载的文件已不存在,可能是被删除");
 
 
$fp = fopen($file,"r");
 
$file_size = filesize($file);
 
//下载文件需要用到的头
 
header("Content-type: application/octet-stream");
 
header("Accept-Ranges: bytes");
 
header("Accept-Length:".$file_size);
 
header("Content-Disposition: attachment; filename=".$down_name);
 
$buffer = 1024;
 
$file_count = 0;
 
//向浏览器返回数据 
 
while(!feof($fp) && $file_count < $file_size){
 
$file_con = fread($fp,$buffer);
 
$file_count += $buffer;
 
echo $file_con;
 
 
fclose($fp);
 
}
 
 
 
?>
 
(责任编辑:最模板)
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
栏目列表
热点内容