基于ThinkPHP3.2.3下载文件(几百M以上的)

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

控制器部分 
   function downfile($fileurl='http://www.fuhuaqi.com/Upload/activity/mongo.rar')
    {
//        下载本地方式一:会受到文件大小限制
//        ob_start();
//        $filename=$fileurl;
//        $date=date("Ymd-H:i:m");
//        header( "Content-type:  application/octet-stream ");
//        header( "Accept-Ranges:  bytes ");
//        header( "Content-Disposition:  attachment;  filename= {$date}.zip");
//        $size=@readfile($filename);
//        header( "Accept-Length: " .$size);

//        下载本地方式二:目前下载500M左右的都没问题,就是时间太慢
        set_time_limit(0);
        ini_set('memory_limit', '512M');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($fileurl));
        header('Content-Transfer-Encoding: binary');
        ob_end_clean();
        $size = readfile($fileurl);
        header( "Accept-Length: " .$size);
    }

方法三:下载之后在指定的地方,但是没有返回,成功之后只是一个空页面,使用TP的HTTP类

//下载之后在指定的地方,但是没有返回,只是一个空页面
    public function downmy()
    {

        $tu = time() . 'mygoods' . ".zip";
        $Http = new Http();
        $Http->curlDownload("http://www.fuhuaqi.com/Upload/activity/111111.zip", "./Upload/activity/".$tu);
        $url = "http://www.fuhuaqi.com/Upload/activity/".$tu;
        return $url;
    }

视图部分

下载

效果

基于ThinkPHP3.2.3下载文件(几百M以上的)_第1张图片

修改之后:

function downfile($fileurl='http://www.fuhuaqi.com/Upload/activity/mongo.rar')
{

    ob_start();
    set_time_limit(0);
    ini_set('memory_limit', '512M');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($fileurl));
    header('Content-Transfer-Encoding: binary');
    ob_end_clean();
    $size = readfile($fileurl);
    header( "Accept-Length: " .$size);
}

修改1.0.1

function downfile()
{
    ob_start();
    $id = I('get.id');
    $bpapplys = D('Bpapply')->selectOne($id);
    //上线后需要修改路径为绝对路径,否则下载不全
    $fileurl="http://www.fuhuaqi.com/Upload/bpapply/".$bpapplys['bp_bpimg'];
    set_time_limit(0);
    ini_set('memory_limit', '512M');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($fileurl));
    header('Content-Transfer-Encoding: binary');
    ob_end_clean();
    $size = readfile($fileurl);
    header( "Accept-Length: " .$size);
}

转载于:https://my.oschina.net/botkenni/blog/825921

你可能感兴趣的:(基于ThinkPHP3.2.3下载文件(几百M以上的))