<?php //1.txt内容“1234567890” socketData('127.0.0.1','/1.txt',80,"RANGE:bytes=0-0\r\n"); socketData('127.0.0.1','/1.txt',80,"RANGE:bytes=9-9\r\n"); 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\r\n"; $out .= 'Host: '.$host."\r\n"; $out .= $headers; if(strlen($data)>=3){ $out .= 'Content-Length: '.strlen($data)."\r\n"; $out .= 'Content-Type: application/x-www-form-urlencoded'."\r\n"; } $out .= "Connection: Close\r\n\r\n".$data; fwrite($fp, $out); $rtn = ""; while (!feof($fp)) { $get = fread($fp, 1024); $rtn.= $get; } fclose($fp); } $strs = explode("\r\n\r\n", $rtn); echo $strs[1].chr(10); }
C:\>php trange.php
1
0
从结果来看,range和数组操作类似,以0为最小的下标(这里该叫起点),count-1为最大下标。