首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >php函数中ftell的输出

php函数中ftell的输出
EN

Stack Overflow用户
提问于 2015-12-24 10:16:13
回答 1查看 1.3K关注 0票数 18

下面是我的代码:

代码语言:javascript
复制
<?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);
    }
?>

结果如下:

代码语言:javascript
复制
    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

Content-Length可能和我的不一样--518264,它会在你执行代码时动态改变,这与讨论无关。为什么结果不是如下所示?

代码语言:javascript
复制
518264
1024
201024
202048
402048
403072

请解释文件指针对fgets、ftell和fseek函数的作用。

EN

回答 1

Stack Overflow用户

发布于 2015-12-31 16:44:17

使用PHP函数stream_get_meta_data()查看您打开的流是否可查找:

代码语言:javascript
复制
$url="http://www.sina.com.cn";
$handle = @fopen($url, "r");

$meta_data = stream_get_meta_data($handle);
var_dump($meta_data['seekable']);

// It prints `bool(false)`

该流不可查找。这意味着函数fseek()ftell()rewind()具有意想不到的(可能不一致的)行为。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34446430

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档