PHP Filesystem fseek() Function
The PHP Filesystem fseek() function is used to seek in an open file. This function can move file pointer from its current position to a new position forward or backward, given by the number of bytes. This function can return 0 on success or -1 on failure. Seeking past EOF can not produce an error.
Syntax
Below is the syntax of the PHP Filesystem fseek() function −
int fseek ( resource $handle , int $offset [, int $whence = SEEK_SET ] )
Parameters
Here are the required and optional parameters of the fseek() function −
| Sr.No | Parameter & Description |
|---|---|
| 1 |
handle(Required) This is a handle to the file opened using fopen(). |
| 2 |
offset(Required) This is the number of bytes you want to move the pointer by. |
| 3 |
whence(Optional) This tells PHP where to start counting the offset from. It can be one of three values − |
Return Value
If successful, fseek() returns 0 and if there is an error, it returns -1.
PHP Version
The fseek() function was first introduced as part of core PHP 4 and work well with the PHP 5, PHP 7, PHP 8.
Example
The PHP Filesystem fseek() Function is used to show the basic usage in this example. View the following PHP code −
<?php
$file = fopen("/Path/To/The/File", "mode");
echo fseek($file, 10, SEEK_CUR);
?>
Example
This code shows how to read a text file from start to finish, starting at the beginning and ending at the end by using fseek() function. The code also uses fopen() and fgets() functions.
<?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
Here is the output of the above code −
Tutorialspoint Tutorialspoint
Example
This example shows how to use fseek() to shift the pointer to the end of the file in order to read the last line of a file.
<?php
//Open the file
$file = fopen("data.txt", "r");
//Move the file pointer
fseek($file, 0, SEEK_END);
//Read the last line
$lastLine = fgets($file);
//Print the last line
echo $lastLine;
//Close the file
fclose($file);
?>
Output
This will produce the following result −
Tutorialspoint
Example
This sample shows how to read a file using fseek() function that has had the first 50 characters skipped. So for this we have used SEEK_SET parameter.
<?php
$file = fopen("myfile.txt", "r");
//Skip 50 characters
fseek($file, 50, SEEK_SET);
//Store content after skipping 50 characters
$contentAfterSkipping = fgets($file);
//Print the result
echo $contentAfterSkipping;
//Close the file
fclose($file);
?>
Output
This will produce the following result after skipping 50 characters −
ork is very important.
Example
This example demonstrates how to write data to designated places inside a file with the help of fseek() function.
<?php
$file = fopen("myfile.txt", "r+");
fwrite($file, "Important note:");
fseek($file, 15, SEEK_SET);
fwrite($file, " Don't forget to submit report");
fclose($file);
?>
Output
This will produce the following result −
Hello World!!!!! Today is 6 June 2024 and I am working on PHP functions. Don't forget to submit report
Note
The starting position can be designated as SEEK_SET (the starting point of the file), SEEK_END (the end of the file), or SEEK_CUR (the pointer's current address). SEEK_SET is assumed by default if it is not provided.
Summary
Use PHP's fseek() function to move the file pointer to a given spot inside a file. Understanding how to use PHP successfully is necessary in order to access and edit file contents in applications.