Search code examples
phpfopen

PHP: fopen() file pointer at the chosen line


How to place a file pointer in the choosen line (e.g. 1 lime from the bottom) when opening file with fopen()?


Solution

  • If you know the offset within the file of the data you want you could always fseek () to it. Of course the problem is discovering where you need to fseek to within the file. If you can use SEEK_END as an option then the offset will be relative to the end of the file instead of the beginning, which may be helpful for you.

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