• PHP Video Tutorials

PHP - Function fseek()



The fseek() function can seek in an open file. This function can move file pointer from its current position to a new position forward or backward, specified by the number of bytes. This function can return 0 on success or -1 on failure. Seeking past EOF can't produce an error.

Syntax

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

The fseek() function can set the file position indicator for the file referenced by the handle. A new position measured in bytes from the beginning of a file has obtained by adding offset to the position specified by whence.

Example

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

Output

Tutorialspoint
Tutorialspoint
php_function_reference.htm
Advertisements