fseek

int fseek(resource file_handle, int offset) 

Seeks in a file.

Returns:

0 on success; -1 on error

Description:

Moves file pointer from its current position to a new position, forward or backward, specified by the number of bytes. You can determine the current position by using ftell(). This function doesn’t operate on file pointers opened using FTP or HTTP. Note that seeking past EOF is not considered an error, but the file position is changed (even if invalid).When trying to seek before the start of the file, fseek() fails.

Example:

Print every second character from a file
$fh = fopen("test.txt", "r"); 

$a = 0; 

while(!feof($fh)) 
{
    fseek($fh, $a); 
    echo (fgetc($fh)); 
    $a = $a + 2; 
} 

fclose($fh); 

Get PHP Functions Essential Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.