几个php文件下载实例代码

一个简单的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);

}

?>

 

注释比较详细的代码:

<?php

 //文件下载,下载一张图片

 //$file_name="Angel.mp3";

 $file_name="bjnihao.jpg";  //出现中文 程序无法完成下载 提示:文件不存在 

 //对文件进行转码(PHP文件函数 比较古老 需对中文码转成 gb2312)

 //iconv — Convert string to requested character encoding

 //by www.jb51.net

 $file_name=iconv("utf-8","gb2312",$file_name);

 //设置文件下载路径(相对路径)

 //$file_path="./dowm/".$file_name;

 //使用绝对路径

 $file_path=$_SERVER['DOCUMENT_ROOT']."/http/dowm/".$file_name;

 //打开文件---先判断再操作

 if(!file_exists($file_path)){

 

  echo "文件不存在";

  return ; //直接退出

 }

 //存在--打开文件

 $fp=fopen($file_path,"r");

 

 //获取文件大小

 $file_size=filesize($file_path);

 //http 下载需要的响应头 

 header("Content-type: application/octet-stream"); //返回的文件 

 header("Accept-Ranges: bytes");   //按照字节大小返回

 header("Accept-Length: $file_size"); //返回文件大小

 header("Content-Disposition: attachment; filename=".$file_name);//这里客户端的弹出对话框,对应的文件名

 //向客户端返回数据

 //设置大小输出

 $buffer=1024;

 //为了下载安全,我们最好做一个文件字节读取计数器

 $file_count=0;

 //判断文件指针是否到了文件结束的位置(读取文件是否结束)

 while(!feof($fp) && ($file_size-$file_count)>0){

  $file_data=fread($fp,$buffer);

  //统计读取多少个字节数

  $file_count+=$buffer;

  //把部分数据返回给浏览器

  echo $file_data;

 }

 //关闭文件

 fclose($fp);

?>

封装函数:

<?php

 /*

  封装函数:

  参数说明----$file_name:文件名

     $file_sub_dir:文件下载的子路径 

 */

 function file_dowm($file_name,$file_sub_dir){

  //文件转码

  $file_name=iconv("utf-8","gb2312",$file_name);

  //使用绝对路径

  $file_path=$_SERVER['DOCUMENT_ROOT']."$file_sub_dir".$file_name;

  //打开文件---先判断再操作

  if(!file_exists($file_path)){

 

   echo "文件不存在";

   return ; //直接退出

  }

  //存在--打开文件

  $fp=fopen($file_path,"r");

 

  //获取文件大小

  $file_size=filesize($file_path);

  /*

  //这里可以设置超过多大不能下载

  if($file_size>50){

   echo "文件太大不能下载";

   return ;

  }*/

  //http 下载需要的响应头 

  header("Content-type: application/octet-stream"); //返回的文件 

  header("Accept-Ranges: bytes");   //按照字节大小返回

  header("Accept-Length: $file_size"); //返回文件大小

  header("Content-Disposition: attachment; filename=".$file_name);//这里客户端的弹出对话框,对应的文件名

  //向客户端返回数据

  //设置大小输出

  $buffer=1024;

  //为了下载安全,我们最好做一个文件字节读取计数器

  $file_count=0;

  //判断文件指针是否到了文件结束的位置(读取文件是否结束)

  while(!feof($fp) && ($file_size-$file_count)>0){

   $file_data=fread($fp,$buffer);

   //统计读取多少个字节数

   $file_count+=$buffer;

   //把部分数据返回给浏览器

   echo $file_data;

  }

  //关闭文件

  fclose($fp);

 }

 

 file_dowm("bjnihao.jpg","/http/dowm/");

?>

您可能感兴趣的文章:
php header()函数的简单例子
php中header函数的用法举例详解
php header 使用详解
php header函数 文件下载时直接提示保存的代码
php header函数实现文本文件下载的方法
php header头信息应用举例
php 文件头部(header)信息详解
php使用header发送各种类型文件下载的例子
PHP header()函数使用详解
PHP中HEADER头消息详解

另一个代码:

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
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值