
- PHP - Home
- PHP - Roadmap
- PHP - Introduction
- PHP - Installation
- PHP - History
- PHP - Features
- PHP - Syntax
- PHP - Hello World
- PHP - Comments
- PHP - Variables
- PHP - Echo/Print
- PHP - var_dump
- PHP - $ and $$ Variables
- PHP - Constants
- PHP - Magic Constants
- PHP - Data Types
- PHP - Type Casting
- PHP - Type Juggling
- PHP - Strings
- PHP - Boolean
- PHP - Integers
- PHP - Files & I/O
- PHP - Maths Functions
- PHP - Heredoc & Nowdoc
- PHP - Compound Types
- PHP - File Include
- PHP - Date & Time
- PHP - Scalar Type Declarations
- PHP - Return Type Declarations
- PHP - Operators
- PHP - Arithmetic Operators
- PHP - Comparison Operators
- PHP - Logical Operators
- PHP - Assignment Operators
- PHP - String Operators
- PHP - Array Operators
- PHP - Conditional Operators
- PHP - Spread Operator
- PHP - Null Coalescing Operator
- PHP - Spaceship Operator
- PHP Control Statements
- PHP - Decision Making
- PHP - If…Else Statement
- PHP - Switch Statement
- PHP - Loop Types
- PHP - For Loop
- PHP - Foreach Loop
- PHP - While Loop
- PHP - Do…While Loop
- PHP - Break Statement
- PHP - Continue Statement
- PHP Arrays
- PHP - Arrays
- PHP - Indexed Array
- PHP - Associative Array
- PHP - Multidimensional Array
- PHP - Array Functions
- PHP - Constant Arrays
- PHP Functions
- PHP - Functions
- PHP - Function Parameters
- PHP - Call by value
- PHP - Call by Reference
- PHP - Default Arguments
- PHP - Named Arguments
- PHP - Variable Arguments
- PHP - Returning Values
- PHP - Passing Functions
- PHP - Recursive Functions
- PHP - Type Hints
- PHP - Variable Scope
- PHP - Strict Typing
- PHP - Anonymous Functions
- PHP - Arrow Functions
- PHP - Variable Functions
- PHP - Local Variables
- PHP - Global Variables
- PHP Superglobals
- PHP - Superglobals
- PHP - $GLOBALS
- PHP - $_SERVER
- PHP - $_REQUEST
- PHP - $_POST
- PHP - $_GET
- PHP - $_FILES
- PHP - $_ENV
- PHP - $_COOKIE
- PHP - $_SESSION
- PHP File Handling
- PHP - File Handling
- PHP - Open File
- PHP - Read File
- PHP - Write File
- PHP - File Existence
- PHP - Download File
- PHP - Copy File
- PHP - Append File
- PHP - Delete File
- PHP - Handle CSV File
- PHP - File Permissions
- PHP - Create Directory
- PHP - Listing Files
- Object Oriented PHP
- PHP - Object Oriented Programming
- PHP - Classes and Objects
- PHP - Constructor and Destructor
- PHP - Access Modifiers
- PHP - Inheritance
- PHP - Class Constants
- PHP - Abstract Classes
- PHP - Interfaces
- PHP - Traits
- PHP - Static Methods
- PHP - Static Properties
- PHP - Namespaces
- PHP - Object Iteration
- PHP - Encapsulation
- PHP - Final Keyword
- PHP - Overloading
- PHP - Cloning Objects
- PHP - Anonymous Classes
- PHP Web Development
- PHP - Web Concepts
- PHP - Form Handling
- PHP - Form Validation
- PHP - Form Email/URL
- PHP - Complete Form
- PHP - File Inclusion
- PHP - GET & POST
- PHP - File Uploading
- PHP - Cookies
- PHP - Sessions
- PHP - Session Options
- PHP - Sending Emails
- PHP - Sanitize Input
- PHP - Post-Redirect-Get (PRG)
- PHP - Flash Messages
- PHP AJAX
- PHP - AJAX Introduction
- PHP - AJAX Search
- PHP - AJAX XML Parser
- PHP - AJAX Auto Complete Search
- PHP - AJAX RSS Feed Example
- PHP XML
- PHP - XML Introduction
- PHP - Simple XML Parser
- PHP - SAX Parser Example
- PHP - DOM Parser Example
- PHP Login Example
- PHP - Login Example
- PHP - Facebook Login
- PHP - Paypal Integration
- PHP - MySQL Login
- PHP Advanced
- PHP - MySQL
- PHP.INI File Configuration
- PHP - Array Destructuring
- PHP - Coding Standard
- PHP - Regular Expression
- PHP - Error Handling
- PHP - Try…Catch
- PHP - Bugs Debugging
- PHP - For C Developers
- PHP - For PERL Developers
- PHP - Frameworks
- PHP - Core PHP vs Frame Works
- PHP - Design Patterns
- PHP - Filters
- PHP - JSON
- PHP - Exceptions
- PHP - Special Types
- PHP - Hashing
- PHP - Encryption
- PHP - is_null() Function
- PHP - System Calls
- PHP - HTTP Authentication
- PHP - Swapping Variables
- PHP - Closure::call()
- PHP - Filtered unserialize()
- PHP - IntlChar
- PHP - CSPRNG
- PHP - Expectations
- PHP - Use Statement
- PHP - Integer Division
- PHP - Deprecated Features
- PHP - Removed Extensions & SAPIs
- PHP - PEAR
- PHP - CSRF
- PHP - FastCGI Process
- PHP - PDO Extension
- PHP - Built-In Functions
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.