Here is my code:
<?php
$url="http://www.sina.com.cn";
$handle = @fopen($url, "r");
$len=get_headers($url,true);
print_r($len);
echo $len['Content-Length']."\n";
if ($handle) {
while (($buffer = fgets($handle,1024)) !== false) {
echo ftell($handle)."\n";
fseek($handle,200000,SEEK_CUR);
echo ftell($handle)."\n";
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
}
?>
The result is as below:
Array
(
[0] => HTTP/1.1 200 OK
[Content-Type] => text/html
[Vary] => Accept-Encoding
[X-Powered-By] => shci_v1.03
[Server] => nginx
[Date] => Thu, 24 Dec 2015 04:03:39 GMT
[Last-Modified] => Thu, 24 Dec 2015 04:01:28 GMT
[Expires] => Thu, 24 Dec 2015 04:04:39 GMT
[Cache-Control] => max-age=60
[Age] => 32
[Content-Length] => 518264
[X-Cache] => HIT from xidan33-99.sina.com.cn
[Connection] => close
)
518264
16
200016
200058
400058
400065
518264
The Content-Length maybe not the same as mine--518264,it will be changed dynamically when you execute the code,it does no matter for the discussion. Why the result is not the following?
518264
1024
201024
202048
402048
403072
please explain the action of file pointer on fgets and ftell and fseek function .
fseek()
, it should be-1
according to de docs ( ar2.php.net/manual/en/function.fseek.php ) – Logician