Home Work PHP Reading In Files With PHP
Reading In Files With PHP PDF Print E-mail
Work - PHP
Written by mbrock   
Monday, 02 June 2008 20:57

PHPHere 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>".

&lt;?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);  
}
?&gt;
 

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.

 

 

Add your comment

Your name:
Your email:
Your website:
Subject:
Comment:
  The word for verification. Lowercase letters only with no spaces.
Word verification:
mbrock.com
Copyright © 2012 mbrock.com. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.