Using the PHP Function feof() to Work With Streams
Streams are a powerful tool in PHP, allowing on-demand access to data. They also allow for a number of additional operations to be performed on data while it's being read or written to the stream. In this article we'll look at a couple of simple examples of working with streams and then move on to more advanced uses, including using the built-in filter functions, tidy_parse_file and xml_parse.
For our example, we'll work with two files: a file in memory, and another file stored in a temporary directory. The first file will contain text, and the second will contain images (as shown below). We'll use a pair of streams to read each line of text from the original file into the destination file. This will be done with fgets() and stream_get_line(). Once the process is complete, we'll need to close the streams and the files.
The feof() function is an inherent PHP function that checks a file pointer to see if it's reached the end of the file. It's useful when you don't know the size of a file in advance and you need to loop through the entire contents.
Other functions that you may want to become familiar with while working with streams include fgets() and fgetc() for reading single lines of text, fscanf for converting field data into variables and fgetss which strips out all HTML and PHP tags and leaves the raw data. Each of these tools is useful in their own way and depending on the task you are performing, one of them may be more efficient than another.