Mastering PHP’s fseek(): How to Move File Pointers Efficiently

Learn how PHP’s fseek() function positions file pointers, understand its syntax, parameters like offset and whence, explore SEEK_SET, SEEK_CUR, SEEK_END options, and see a complete example that reads, writes, and manipulates files using fseek alongside fopen, fread, and fwrite.

php Courses
php Courses
php Courses
Mastering PHP’s fseek(): How to Move File Pointers Efficiently

The fseek() function moves the file pointer to a specified location, allowing subsequent read, write, or append operations on an opened file.

fseek() function syntax

fseek(file, offset, whence)
file

: required. The file resource opened by fopen() whose pointer you want to move. offset: required. Number of bytes to move the pointer; positive values move forward, negative values move backward, zero leaves the pointer unchanged. whence: optional. Determines how the offset is interpreted. Possible values are: SEEK_SET (default): set pointer to the beginning of the file. SEEK_CUR: set pointer relative to the current position. SEEK_END: set pointer relative to the end of the file.

Example of using fseek()

<?php
$file = fopen("example.txt", "r+");
if ($file) {
    // Move pointer to the start of the file
    fseek($file, 0, SEEK_SET);

    // Read and output the first 10 bytes
    echo fread($file, 10);

    // Move pointer to the end of the file
    fseek($file, 0, SEEK_END);

    // Write a new line to the file
    fwrite($file, "This is a new line.");

    // Move pointer back to the start
    fseek($file, 0, SEEK_SET);

    // Read and output the entire file
    echo fread($file, filesize("example.txt"));

    // Close the file
    fclose($file);
}
?>

In this code, fopen() opens a file in read‑write mode, fseek() moves the pointer to various positions, fread() reads data, and fwrite() appends new content. Finally, the file is closed with fclose().

The example demonstrates that fseek() is highly flexible, enabling you to locate any position within a file for reading, writing, or modifying data, making file handling more efficient and versatile.

Overall, fseek() is an essential PHP function for precise file manipulation, allowing developers to control file pointers and perform a wide range of file operations.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendPHPfile-handlingfseek
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.