找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2785|回复: 0
打印 上一主题 下一主题
收起左侧

php 文件下载源码

[复制链接]
跳转到指定楼层
楼主
ID:90084 发表于 2015-9-14 20:39 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
下载文件的方法:
实列一:
<?php
        header("Content-Type: text/html;charset=utf-8");

        $file_name ="jdao.png";

        //使用相对路径
        //    $file_path = "./pic/".$file_name;

        // 使用绝对路径,下载速度要更快些
        $file_path = $_SERVER['DOCUMENT_ROOT']."/pic/".$file_name;  


        if(!file_exists($file_path)){
          echo "文件不存在";
          return;
        }

        $fp = fopen($file_path,"r");
        $file_size = filesize($file_path);
        //echo $file_size;

        header("Content-Type: application/octet-stream");  //返回为文件形式
        header("Accept-Ranges: bytes");
        header("Accept-Length: $file_size");  
        header("Content-Disposition: attachment; filecolor:#c82829">$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);

        // echo $file_path; 这句话不能放在下载前,否则就执行不了下载


?>


实例2:
为了兼容中文文件名不出现乱码,并做成函数源码如下:

<?php
    //首行前不能空出
    //文件下载参数说明
    //$file_name: 下载文件名
    //$file_sub_path: 子文件夹路径

   function fileDown($file_name,$file_sub_path){


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


       $file_path =  $_SERVER['DOCUMENT_ROOT'].$file_sub_path .$file_name;


       if(!file_exists($file_path)){
          echo "文件不存在";
          return;
        }

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


        //下载文件的头部分
        header("Content-Type: application/octet-stream");  //返回为文件形式
        header("Accept-Ranges: bytes");
        header("Accept-Length: $file_size");  

       //header("Content-Disposition: attachment; filewidth:14px;color:#8e908c">弹

       //以下为了兼容中文文件名在不同浏览器中显示而做如下修改
        $ua = $_SERVER["HTTP_USER_AGENT"];
        $encoded_filename = urlencode($file_name);
        $encoded_filename = str_replace("+", "%20", $encoded_filename);
        header('Content-Type: application/octet-stream');
        if (preg_match("/MSIE/", $ua)) {
        header('Content-Disposition: attachment; filecolor:#c82829">$encoded_filename . '"');
        } else if (preg_match("/Firefox/", $ua)) {
        header('Content-Disposition: attachment; filename*="utf8\'\'' . $file_name . '"');
        } else {
        header('Content-Disposition: attachment; filecolor:#c82829">$file_name . '"');
        }



        $buffer = 11024;
        $file_count = 0;
        while((!feof($fp)) && ($file_size - $file_count > 0)){

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

            //把数据回送给浏览器
            echo $file_data;

        }


        fclose($fp);
  }


   fileDown("电脑图片.jpg","/pic/");   

?>




测试文件下载
测试中文文件下载

将文件下载封装在类文件里filedown.class.php,源码如下
<?php
    class filedown{
               protected    $file_name;
               public    $file_sub_path;

                function __construct($file_name,$file_sub_path){
                   $this->file_name = $file_name;
                   $this->file_sub_path = $file_sub_path;
                }

               function fileDown(){


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


                   $file_path =  $_SERVER['DOCUMENT_ROOT'].$this->file_sub_path .$this->file_name;


                   if(!file_exists($file_path)){
                      echo "文件不存在";
                      return;
                    }

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


                    //下载文件的头部分
                    header("Content-Type: application/octet-stream");  //返回为文件形式
                    header("Accept-Ranges: bytes");
                    header("Accept-Length: $file_size");  

                   //header("Content-Disposition: attachment; filewidth:14px;color:#8e908c">弹

                   //以下为了兼容中文文件名在不同浏览器中显示而修改的
                    $ua = $_SERVER["HTTP_USER_AGENT"];
                    $encoded_filename = urlencode($this->file_name);
                    $encoded_filename = str_replace("+", "%20", $encoded_filename);
                    header('Content-Type: application/octet-stream');
                    if (preg_match("/MSIE/", $ua)) {
                    header('Content-Disposition: attachment; filecolor:#c82829">$encoded_filename . '"');
                    } else if (preg_match("/Firefox/", $ua)) {
                    header('Content-Disposition: attachment; filename*="utf8\'\'' . $this->file_name . '"');
                    } else {
                    header('Content-Disposition: attachment; filecolor:#c82829">$this->file_name . '"');
                    }



                    $buffer = 11024;
                    $file_count = 0;
                    while((!feof($fp)) && ($file_size - $file_count > 0)){

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

                        //把数据回送给浏览器
                        echo $file_data;

                    }


                    fclose($fp);
              }

    }

//  $file1 = new filedown("电脑图片.jpg","/pic/");
//   $file1->filedown();

?>


演示多文件下载






分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表