关于http断点续传相关的RANGE这个header

<?php
//1.txt内容“1234567890”
socketData('127.0.0.1','/1.txt',80,"RANGE:bytes=0-0
");
socketData('127.0.0.1','/1.txt',80,"RANGE:bytes=9-9
");
function socketData($host,$url,$port = 80,$headers='',$data='')
{
	$fp = fsockopen($host, $port, $errno, $errstr, 15);

	if (!$fp) {
		return false;
	} else {
		$out = ($data?'POST':'GET').' '.$url." HTTP/1.1
";
		$out .= 'Host: '.$host."
";
		$out .= $headers;		

		if(strlen($data)>=3){
			$out .= 'Content-Length: '.strlen($data)."
";
			$out .= 'Content-Type: application/x-www-form-urlencoded'."
";
		}
		
		$out .= "Connection: Close

".$data;
		fwrite($fp, $out);
		$rtn = "";
		
		while (!feof($fp)) {
			$get = fread($fp, 1024);
			$rtn.= $get;
		}
		
		fclose($fp);
	}
	$strs = explode("

", $rtn);
	echo $strs[1].chr(10);
}

C:>php trange.php
1
0

从结果来看,range和数组操作类似,以0为最小的下标(这里该叫起点),count-1为最大下标。


原文地址:https://www.cnblogs.com/lein317/p/5067550.html