php實現大文件下載

原理:

 

逐條查詢,然後分批ob_flush輸出到網頁 ,當然重點還是設置http請求頭,源碼例子

public function exportExcel()
    {
        //將超時時間設置爲沒有限制
        set_time_limit(0);
        $adminID = str2int( $this->input->get('admin_id') );
        $uidStr = trim( $this->input->get('uid') );

        //開始和結束時間
        $begin_time = ($this->input->get("begin_time"));
        $end_time = ($this->input->get("end_time"));

        $reason = ($this->input->get("reason"));

        $uids = explode(',', $uidStr);
        $uids = tool::convertOneLevelArrayType($uids,tool::INT);
        $uids = tool::filterOneLevelEmpty($uids);

        $mGold = cw_gold_log_model::getInstance();

        $guestModel = cw_guest_model::getInstance();

        //條件
        $where = array();$where_in = array();


        $condition = [];
        if (!empty($uids)||$adminID) {
            if($adminID)
            {
                $condition[cw_gold_log_model::F_admin_id] = $adminID;
            }
//            $where = $adminID?array(cw_gold_log_model::F_admin_id => $adminID,):array();
            $where_in = array(cw_gold_log_model::F_uid , $uids,);
//            $filter['where'] = $where;
//            $condition[] = $where;
            if(!empty($uids)){
                $filter['where_in'] = $where_in;
            }
        }

        if(strtotime($begin_time))
        {
            $condition["ctime >"] = strtotime($begin_time);
        }

        if(strtotime($end_time))
        {
            $condition["ctime <="] = (strtotime($end_time)+24*60*60);
        }


        if($reason)
        {
            $condition["type"] = $reason;
        }


        $filter['order'] = array(cw_gold_log_model::F_id,'desc');
        $filter["where"] = $condition;
        // print_r($filter);
        //獲取記錄列表
        $filter["limit"] = 1;
        $filter["start"] = 0;

        //定義http的頭
        header("Cache-Control:");
        header("Cache-Control: public");

        //設置輸出瀏覽器格式  
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");
        header("Content-Type: application/vnd.ms-excel");
        header("Content-Disposition: attachment; filename=excel-doc.xls");
        header("Accept-Ranges: bytes");
        header("Content-Transfer-Encoding: binary");

        //"id,admin_id,uid,gold,type,ctime";
        //這是標頭
        $titles = array('id', '用戶', '管理員', '車輪幣', '原因', '發放時間');

        $titles = self::array_iconv('utf-8', 'gbk', $titles);

        $headers = array();

        if (is_null($this->custom_titles)) {
            if (is_array($titles)) {
                foreach ($titles AS $title) {
                    $headers[] = $title;
                }
            }
            else {
                foreach ($titles AS $title) {
                    $headers[] = $title->name;
                }
            }
        }
        else {
            $keys = array();
            foreach ($titles AS $title) {
                $keys[] = $title->name;
            }
            foreach ($keys AS $key) {
                $headers[] = $this->custom_titles[array_search($key, $keys)];
            }
        }

        //獲取要輸出的headers
        $headers = implode("\t", $headers);

        //清理ob緩衝
        ob_clean();

        print($headers."\n");

        //沖刷緩存
        ob_flush();

        //開始進行數據查詢(查詢出第一條)
        while($first = $mGold->glist($filter,cw_gold_log_model::Table))
        {

            //輸出文件過濾導出內容
            $line = '';
            foreach ($first[0] AS  $key=>$value) {
                if (!isset($value) OR $value == '') {
                    $value = "\t";
                }
                else {

                    //轉化管理員id
                    if($key == "admin_id")
                    {
                        if($value)
                        {
                            $info = $guestModel->getByUid($value);
                            $value = isset($info["nick"]) ? $info["nick"] : "";
                        }else{
                            $value = "系統用戶";
                        }
                    }elseif($key == "uid")
                    {
                        if($value)
                        {
                            $info = $guestModel->getByUid($value);
                            $value = isset($info["nick"]) ? $info["nick"] : "";
                        }else{
                            $value = "系統用戶";
                        }
                    }elseif($key == "type")
                    {
                        $value = $mGold->get_reason($first[0]);
                    }else if ($key == "ctime")
                    {
                        $value = $value ? date("Y-m-d H:i",$value) : "";
                    }elseif($key == "gold")
                    {
                    }elseif($key == "id"){

                    }else{
                        continue;
                    }
                    $value = mb_convert_encoding($value, 'gbk', 'utf-8');
                    $value = str_replace('"', '""', $value);
                    $value = '"' . $value . '"' . "\t";
                }
                $line .= $value;
            }

            print(str_replace("\r", "", trim($line) . "\n"));
            ob_flush();
            $compare_id = $first[0]["id"];
            $filter["where"]["id < "] = $compare_id;
        }
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章