PHP | fseek() Function

PHP fseek() Function


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP fseek() Function.

The PHP fseek() function seeks in a open file. This function can move the file pointer from it's current position to a new position forward, or backward, which is specified by the number of bytes. Seeking past EOF can not produce an error.

Syntax

Following below is the syntax to use this function -

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


The built-in PHP fseek() function sets the file position indicator for the file referenced by the handle. A new position measured in bytes from the starting of a file is obtained by adding offset to the position specified by whence.

Return Value

This PHP function returns 0 on success or -1 on failure.

Example

Try out the below 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

When the above code is executed, it will produce the following result -

Webdesigntutorialz
Webdesigntutorialz


Alright guys! This is where we are going to be rounding up for this tutorial post. In our next tutorial, we are going to be discussing about the PHP fstat() Function.

Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.