PHP and MySQL Web Development - P20: PHP and MySQL Web Development teaches the reader to develop dynamic, secure, commercial Web sites. Using the same accessible, popular teaching style of the first edition, this best-selling book has been updated to reflect the rapidly changing landscape of MySQL and PHP. | 62 Chapter 2 Storing and Retrieving Data Opening a File for Reading fopen Again we open the file using fopen . In this case we are opening the file for reading only so we use the file mode r fp fopen DOCUMENT_ROOT . orders r Knowing When to Stop feof In this example we use a while loop to read from the file until the end of the file is while loop tests for the end of the file using the feof function while feof fp The feof function takes a file pointer as its single parameter. It will return true if the file pointer is at the end of the file. Although the name might seem strange it is easy to remember if you know that feof stands for File End Of File. In this case and generally when reading from a file we read from the file until EOF is reached. Reading a Line at a Time fgets fgetss and fgetcsv In our example we use the fgets function to read from the file order fgets fp 999 This function is used to read one line at a time from a file. In this case it will read until it encounters a newline character n encounters an EOF or has read 998 bytes from the file. The maximum length read is the length specified minus one byte. There are many different functions that can be used to read from fgets function is useful when dealing with files that contain plain text that we want to deal with in chunks. An interesting variation on fgets is fgetss which has the following prototype string fgetss int fp int length string allowable_tags This is very similar to fgets except that it will strip out any PHP and HTML tags found in the string. If you want to leave any particular tags in you can include them in the allowable_tags would use fgetss for safety when reading a file written by somebody else or containing user input. Allowing unrestricted HTML code in the file could mess up your carefully planned formatting. Allowing unrestricted PHP could give a malicious user almost free rein on your server. The function fgetcsv is another variation .