PHP 大文件下载(方式1 不支持断点续传)

简介:
 public function download(){

    $id = input('id');
    $item = Db::name('imgsubmit')->where(['id'=>$id])->find();
    Db::name('imgsubmit')->where(['id'=>$id])->update(['status'=>1]);
    $tt = ltrim($item['zip'],'uploads/');
    $path = config('upload_path').DS.$tt;
    $arr = explode('/',$path);
    $iname=$item['username'].'_'.$item['order_no'];
    /*$filename = $arr[count($arr)-1];*/
    $filename=$iname.'.zip';
    //$file  =  fopen($path, "rb");

        if (file_exists($path)) {
         $filesize=filesize($path);
         header('Content-Description:File Transfer');
         header("Content-Type:application/octet-stream");
         header('Content-Transfer-Encoding:binary');
         header("Accept-Ranges: bytes");
         header('Expires:0');
         header('Cache-Control:must-revalidate');
         header('Pragma:public');
         header("Content-Length:".$filesize);
         header("Content-Disposition:attachment;filename=".$filename);
        //以下内容是引用网友的代码,由于原来的出处找不到了,在此表示万分感谢和抱歉。
        //打开文件 
        $fp = fopen($path, "rb"); 
        //设置指针位置 
        ob_start();
        fseek($fp,0); 
        //虚幻输出 
        while (!feof($fp)) { 
            //设置文件最长执行时间 
            set_time_limit(0); 
            print (fread($fp, 1024 * 8)); //输出文件 
            flush(); //输出缓冲 
            ob_flush(); 
        } 
        fclose($fp); 
        exit ();}else{echo "文件不存在!!!";die();}

}







本文转自lb沫51CTO博客,原文链接:http://blog.51cto.com/13562606/2066555,如需转载请自行联系原作者
相关文章
|
1月前
thinkphp5.1隐藏index.php入口文件
thinkphp5.1隐藏index.php入口文件
30 0
thinkphp5.1隐藏index.php入口文件
|
3月前
|
PHP 数据安全/隐私保护
php案例:判断这个文件是什么编程语言代码的文件(判断java或者php)
php案例:判断这个文件是什么编程语言代码的文件(判断java或者php)
php案例:判断这个文件是什么编程语言代码的文件(判断java或者php)
php案例:用代码的方式创建目录+文件+写入数据(都由你定)
php案例:用代码的方式创建目录+文件+写入数据(都由你定)
php案例:用代码的方式创建目录+文件+写入数据(都由你定)