File open/read/close
When readfile() is not enough and you need more control, PHP lets you work with files through a file handle. Instead of dumping everything at once, you explicitly open the file, read from it in smaller pieces, and close it when you are done.
Calling fopen() does not give you the contents of the file. It returns a handle that represents an open connection to that file, and the mode you choose (r, w, a, and so on) defines what you are allowed to do with it.
From there, you decide how to read. fread() lets you pull a specific number of bytes, which is useful when you know the size upfront. fgets() reads one line at a time and fits naturally inside loops that process data gradually. To know when there is nothing left to read, you rely on feof(), which tells you when the file pointer has reached the end.
For even finer control, fgetc() reads a single character at a time and advances the pointer one character forward. This is slower, but useful for very low-level parsing or special formatting cases.
Once you are finished, closing the file with fclose() is not optional. It frees resources and prevents subtle bugs. In WordPress code especially, anchoring file paths with __DIR__ keeps things predictable no matter how the file is loaded.