php下载文件的指定位置内容

$fp=fopen($filename,"rb"); 
        $file_size=filesize($filename);
        $date=date("Ymd-H:i:m");
        $size = 1024*1024*50;//50M大小的内容
        $num = ceil($file_size/$size);
        if($i<$num&&$i>=0) { //$i表示每50M一个间隔
            $fp=fopen($filename,"rb");
            Header("Content-type: application/octet-stream"); 
            Header("Accept-Ranges: bytes"); 
            header(sprintf('Content-Length: %u',$size));
            header(sprintf('Content-Range: bytes %s-%s/%s',0,$size-1,$file_size));
            Header("Content-Disposition: attachment; filename={$date}_".$i.".log"); 
            fseek($fp, sprintf('%u',1024*1024*50*$i));
            $buffer=1024; 
            $file_count=0;
            while(!feof($fp) && $file_count<$file_size){ 
                set_time_limit(0);
                $file_con=fgets($fp,$buffer); 
                $file_count+=$buffer;
                echo $file_con; 
            }
            fclose($fp);
        }

关键是:Content-Range,fseek()的应用

 

posted on 2017-03-10 16:17  LSMORPJER  阅读(186)  评论(0)    收藏  举报

导航