php切片下载文件

实际应用场景中下载大文件如果超过nginx和php的限制就会报错,可以使用php切片下载

$filepath = "./Uploads/slicefile/test.rar";
		$file_size = filesize($filepath);
		$fileName = "2.rar";
		header("Content-type: application/octet-stream");
		header("Accept-Ranges: bytes");
		header("Accept-Length: $file_size");
		header("Content-Disposition: attachment; filename=".$fileName);
		$fp = fopen($filepath,"r");
		$buffer_size = 10;
		$cur_pos = 0;

		$fout = fopen("php://output","w");
		while(!feof($fp)&&$file_size-$cur_pos>=$buffer_size){
			$buffer = fread($fp,$buffer_size);
			$cur_pos += $buffer_size;
			fwrite($fout,$buffer);
			//echo $buffer;这样也可以
    	}
		fclose($fp);

posted on 2024-06-19 17:03  孤灯引路人  阅读(5)  评论(0编辑  收藏  举报

导航