牛牛在线工具

streamWrapper::stream_tell()

收录 8868 个 PHP 函数,含用途说明与用法示例。

streamWrapper::stream_tell()

获取当前文件指针的位置

函数名:streamWrapper::stream_tell() 适用版本:PHP 5 >= 5.1.0, PHP 7 函数描述:stream_tell()函数用于获取当前文件指针的位置。 用法: int streamWrapper::stream_tell ( void ) 参数: 该函数没有参数。 返回值: 返回一个整数,表示当前文件指针的位置。如果发生错误,返回false。 示例: <?php class MyStreamWrapper { private $position = 0; private $data = "Hello, World!"; public function stream_open($path, $mode, $options, &$opened_path) { return true; } public function stream_read($count) { $data = substr($this->data, $this->position, $count); $this->position += strlen($data); return $data; } public function stream_write($data) { $this->data .= $data; $this->position += strlen($data); return strlen($data); } public function stream_tell() { return $this->position; } } stream_wrapper_register("mywrapper", "MyStreamWrapper"); $file = fopen("mywrapper://test.txt", "w"); fwrite($file, "Hello"); echo "Current position: " . ftell($file) . "\n"; // 输出:Current position: 5 fclose($file); ?> 在上面的示例中,我们定义了一个自定义的流(wrapper)类MyStreamWrapper,并注册为mywrapper。在stream_tell()方法中,我们简单地返回了当前的位置$this->position。然后我们使用fopen()函数打开了mywrapper://test.txt文件,并使用fwrite()写入了5个字符。最后,我们使用ftell()函数获取当前文件指针的位置,并输出到屏幕上。