找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

帖子
查看: 2764|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

php 文件下載源碼

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:90084 發(fā)表于 2015-9-14 20:39 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
下載文件的方法:
實(shí)列一:
<?php
        header("Content-Type: text/html;charset=utf-8");

        $file_name ="jdao.png";

        //使用相對(duì)路徑
        //    $file_path = "./pic/".$file_name;

        // 使用絕對(duì)路徑,下載速度要更快些
        $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); //彈出下載對(duì)話框

        //向客戶端發(fā)送數(shù)據(jù)


        $buffer = 1024;
        $file_count =0;
        while((!feof($fp)) && ($file_size - $file_count >0)){
            $file_data = fread($fp,$buffer);
            $file_count += $buffer;
            //把數(shù)據(jù)回送給瀏覽器
            echo $file_data;

        }

        fclose($fp);

        // echo $file_path; 這句話不能放在下載前,否則就執(zhí)行不了下載


?>


實(shí)例2:
為了兼容中文文件名不出現(xiàn)亂碼,并做成函數(shù)源碼如下:

<?php
    //首行前不能空出
    //文件下載參數(shù)說明
    //$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">彈對(duì)

       //以下為了兼容中文文件名在不同瀏覽器中顯示而做如下修改
        $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;

            //把數(shù)據(jù)回送給瀏覽器
            echo $file_data;

        }


        fclose($fp);
  }


   fileDown("電腦圖片.jpg","/pic/");   

?>




測(cè)試文件下載
測(cè)試中文文件下載

將文件下載封裝在類文件里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">彈對(duì)

                   //以下為了兼容中文文件名在不同瀏覽器中顯示而修改的
                    $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;

                        //把數(shù)據(jù)回送給瀏覽器
                        echo $file_data;

                    }


                    fclose($fp);
              }

    }

//  $file1 = new filedown("電腦圖片.jpg","/pic/");
//   $file1->filedown();

?>


演示多文件下載






分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表