English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP fseek() Function Usage and Example

PHP Filesystem Reference Manual

The fseek() function can search in the opened file. This function can move the file pointer from its current position to a new position, which is specified by the number of bytes. If successful, this function can return 0, or it can return-1. Searching for the past EOF does not produce an error.

Syntax

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

The fseek() function sets the file pointer position in the file associated with the handle. The new position is measured in bytes from the beginning of the file, which is the position specified by whence plus offset.

Online Example

<?php
   $file = fopen("/PhpProject/sample.txt, "r");
   
   //Read the first line
   echo fgets($file);
   
   //Move back to the beginning of the file
   fseek($file, 0);
   
   echo fgets($file);
   
?>

Output Result

w3codebox
w3codebox

PHP Filesystem Reference Manual