The fseek() function seeks in an open file, by moving the file pointer from its current position to a new position, forward or backward, specified by the number of bytes.
PHP fseek() Function has the following syntax.
fseek(file,offset,whence)
Parameter | Is Required | Description |
---|---|---|
file | Required. | Open file to seek in |
offset | Required. | New position (measured in bytes from the beginning of the file) |
whence | Optional. | Option |
Possible values for whence:
This function returns 0 on success, or -1 on failure. Seeking past EOF will not generate an error.
fseek() moves a file handle pointer to an arbitrary position.
<?PHP// ww w. j av a2s . c o m
$filename = "c:/abc/test.txt";
$handle = fopen($filename, "w+");
fwrite($handle, "java2s.com\n");
rewind($handle);
fseek($handle, 1);
fwrite($handle, "o");
fseek($handle, 2, SEEK_CUR);
fwrite($handle, "e");
fclose($handle);
?>
The first byte of a file is byte 0, and you count upward from there. The second byte is at index 1, the third at index 2, etc.
Find the current position by using ftell().