| Reading In Files With PHP |
|
|
|
| Work - PHP |
| Written by mbrock |
| Monday, 02 June 2008 20:57 |
|
<?php /******************************************************************* * Function: ReadAFile($filename) * Input : a valid filename as a string * Output : prints the contents of the file to the screen * Returns : nothing ********************************************************************/ function ReadAFile($filename) { //open the file for reading $fp=fopen($filename,"r"); //get a line, store it in the //$line variable while($line=fgets($fp)) { print nl2br($line); } //close the file pointer fclose($fp); } ?> I have used this function and slightly modified versions of of it many times to display text files on web pages or to read in XML and RSS feeds.
|





Here is a simple function that opens a file, reads it in a line at a time, and prints the output to the screen. The nl2br statement in the function will convert the newline characters ("\n" or "\r\n" depending on your OS) to HTML breaks, "<br>".