Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

fseek()

Example

Move the file pointer back to the file’s beginning after reading the first line from the open file:

<?php
$file = fopen(“test.txt”,“r”);
// Read first line
echo fgets($file);
// Move back to beginning of file
fseek($file,0);
fclose($file);
?>

Definition and Usage

An open file is sought in with the fseek() function.

This function shifts the file pointer, forward or backward, by the number of bytes, from its present location.

Help: Use ftell() to determine the current location!

Syntax

fseek(fileoffsetwhence)

Parameter Values

 

Parameter

Description

file

Essential. indicates which open file to look for in

offset

Essential. identifies the new location, measured in bytes starting at the file’s beginning.

whence

Not required. SEEK_SET – Set position equal to offset – is one possible value. By Default
SEEK_CUR: Set position to offset plus current location
SEEK_END: Set position to EOF + offset (offset must be negative to advance to a place before EOF).

Technical Details

Return Value:

If successful, 0; if not, -1

PHP Version:

4.0+