fseek() function in PHP


The fseek() function seeks in an open file. It returns 0 on success, else returns -1 on failure.

Syntax

fseek(file_pointer, offset, whence)

Parameters

  • file_pointer − A file pointer created using fopen(). Required.

  • offset − Specify the new position. Required.

  • whence − The following are the values:

    • SEEK_SET - Set position equal to offset bytes. Default.
    • SEEK_CUR - Set position to current location plus offset.
    • SEEK_END - Set position to end-of-file plus offset.

Return

The fseek() function returns 0 on success, else returns -1 on failure.

Example

<?php
   $file_pointer = fopen("new.txt", "w");
   fgets($file_pointer);
   // move back to the beginning of file
   echo fseek($file_pointer, 0);
   fclose($myfile);
?>

Output

0

Updated on: 24-Jun-2020

353 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements