PHP: file pointer fopen () on the selected line

How to place the file pointer in the selected line (for example, 1 lime below) when opening the file with the fopen () function?

+3
source share
4 answers

If you know the offset in the data file you want, you can always fseek () . Of course, the problem is to discover where you need fseek within the file. If you can use SEEK_END as an option, then the offset will refer to the end of the file instead of the beginning, which may be useful to you.

Alternatively, you can use the file () function to load the file data into an array. Each element of the array is a line of the file, so the second and last element in the array will be the element you want.

+4
source

You may have to read the end of the file count line "\ n". Sort of:

function fseek_line($handle, $count) {
  while ((--$count > 0) && (fgets($handle, 4096) !== false)) { }
}

If you need the last line just opened in add mode fopen("file.txt", "a");

+1
source

SplFileObject:: seek, N- .

+1

, . linuxise, .

: fseek() , ?

0

Source: https://habr.com/ru/post/1786144/


All Articles