PHP fseek() 函数用法及示例 - PHP教程

由网友 大卫 发布 阅读 2

PHP fseek() 函数用法及示例 - PHP教程

PHP Filesystem 参考手册

fseek()函数可以在打开的文件中查找。此函数可以将文件指针从其当前位置向前或向后移动到新位置,该位置由字节数指定。成功时此函数可以返回0,失败时可以返回-1。寻找过去的EOF不会产生错误。

语法

int fseek ( resource $handle , int $offset [, int $whence = SEEK_SET ] )

fseek()函数在与 handle 关联的文件中设定文件指针位置。 新位置从文件头开始以字节数度量,是以 whence 指定的位置加上 offset。

在线示例

<?php
   $file = fopen("/PhpProject/sample.txt", "r");
   
   //读第一行
   echo fgets($file);
   
   //移回文件开头
   fseek($file, 0);
   
   echo fgets($file);
   
?>

输出结果

div.cn
div.cn

PHP Filesystem 参考手册

PHP Array 函数手册 PHP preg_replace_callback_array()