• php文件下载


    <?php
        
        /*
        *下载报告
        */
        public function down_report(){
            /*
            $result = array("error"=>1,"content"=>'');
            if(!session('user_id')) {
                $result['error'] = -1;
                $result['content'] = "请先登录";
                $result['url'] = "/mobile/user/login";
                $this->showMsg($result);
            }
            $url = yf_empty($_POST['url'], '', true);
            if(!$url) {
                $result['content'] = "非法参数";
                $this->showMsg($result);
            }
            */
        /*        
            if(1==1){
                $result['error'] = 1;
                $result['content'] = $url;
                $this->showMsg($result);
            }
        */
            
            
            /* 下载开始 */
            $url = yf_empty($_GET['url'], '', true);
            if(!$url){
                echo "文件参数错误";
                exit;
            }
            $filename = basename($url);    //文件名
            $filepath = $_SERVER['DOCUMENT_ROOT'].$url;    //文件绝对路径
           
            if(!file_exists($filepath)){
               $result['error'] = 1;
               $result['content'] = "文件不存在";
            }
           
            $fp = fopen($filepath,"r+");        //打开文件流
            $filesize = filesize($filepath);
           
            //$filearr = explode(".",$filename);
            //$prefix = $filearr[0];    //文件前缀
            //$suxfix = $filearr[1];    //文件后缀
            header("Content-Type:application/octet-stream");
            header("Accept-Ranges:bytes");
            header("Accept-Length:".$filesize);
            header("Content-Disposition:attachment;filename=".$filename);
            header("Pragma:no-cache");    //不缓存
            header("Expires:0");
            $buffer=1024;
            $filecount=0;
           
            //向浏览器返回数据
            while(!feof($fp) && $filecount<$filesize){
               $filecon = fread($fp,$buffer);
               $filecount += $buffer;
               echo $filecon;
            }
            fclose($fp);
        }
        
    ?>
  • 相关阅读:
    将TinyXml快速入门的接口面向对象化
    Java EE6 核心特征:Bean Validation 解析
    风花雪月(三)
    跑步破百
    在VC6中改变导入库名称的方法
    Java EE6 核心特征:Bean Validation 解析
    古哥,一路走好
    boost_1_42在VS环境下的完全编译
    总结一下__declspec(dllimport)的作用
    解决MySQL server has gone away
  • 原文地址:https://www.cnblogs.com/boluoboluo/p/6700453.html
Copyright © 2020-2023  润新知