Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

fseek

Description

The fseek of Filesystem for PHP seeks on a file pointer.

Syntax

fseek(
    resource $stream,
    int $offset,
    int $whence = SEEK_SET
): int

Parameters

stream

A file system pointer resource that is typically created using fopen().

offset

The offset.

To move to a position before the end-of-file, you need to pass a negative value in offset and set whence to SEEK_END.

whence

ConstantDescription
SEEK_SETSet position equal to offset bytes.
SEEK_CURSet position to current location plus offset.
SEEK_ENDSet position to end-of-file plus offset.

Return

Returns 0 on success, -1 otherwise.

Examples

1 · handle offset · 0

<?

$filename = $_SERVER['DOCUMENT_ROOT'] . '/assets/txt/1.txt';
$mode = 'r';

$handle = fopen($filename, $mode);

    // read data
    $data = fgets($handle, 4096);
    echo $data . PHP_EOL;

    // move to the beginning of the file then offset 0 - same as rewind($handle)
    $offset = 0;
    fseek($handle, $offset);

    // read data
    $data = fgets($handle, 4096);
    echo $data;

fclose($handle);

?>
hello
hello

2 · handle offset · 2

<?

$filename = $_SERVER['DOCUMENT_ROOT'] . '/assets/txt/1.txt';
$mode = 'r';

$handle = fopen($filename, $mode);

    // read data
    $data = fgets($handle, 4096);
    echo $data . PHP_EOL;

    // move to the beginning of the file then offset 2
    $offset = 2;
    fseek($handle, $offset);

    // read data
    $data = fgets($handle, 4096);
    echo $data;

fclose($handle);

?>
hello
llo

3 · whence · SEEK_SET

<?

$filename = $_SERVER['DOCUMENT_ROOT'] . '/assets/txt/1.txt';
$mode = 'r';

$handle = fopen($filename, $mode);

    // read data
    $data = fgets($handle, 4096);
    echo $data . PHP_EOL;

    // move to the beginning of the file then offset 2
    $offset = 2;
    $whence = SEEK_SET;
    fseek($handle, $offset, $whence);

    // read data
    $data = fgets($handle, 4096);
    echo $data;

fclose($handle);

?>
hello
llo

4 · whence · SEEK_CUR

<?

$filename = $_SERVER['DOCUMENT_ROOT'] . '/assets/txt/1.txt';
$mode = 'r';

$handle = fopen($filename, $mode);

    // read data
    $data = fgets($handle, 3);
    echo $data . PHP_EOL;

    // move to the current location then offset 2
    $offset = 2;
    $whence = SEEK_CUR;
    fseek($handle, $offset, $whence);

    // read data
    $data = fgets($handle, 4096);
    echo $data;

fclose($handle);

?>
he
o

5 · whence · SEEK_END

<?

$filename = $_SERVER['DOCUMENT_ROOT'] . '/assets/txt/1.txt';
$mode = 'r';

$handle = fopen($filename, $mode);

    // read data
    $data = fgets($handle, 4096);
    echo $data . PHP_EOL;

    // move to the end of the file then offset -2
    $offset = -2;
    $whence = SEEK_END;
    fseek($handle, $offset, $whence);

    // read data
    $data = fgets($handle, 4096);
    echo $data;

fclose($handle);

?>
hello
lo

6 · return

<?

$filename = $_SERVER['DOCUMENT_ROOT'] . '/assets/txt/1.txt';
$mode = 'r';

$handle = fopen($filename, $mode);

    $offset = 2;

    $return = fseek($handle, $offset);

    echo $return;

fclose($handle);

?>
2
HomeMenu