Last updated on November 18th, 2015 at 03:47 am

Simple php code for reading lines
Using the below script you can easily read a txt file. Let the text file be mistonline.in.txt and you are using fopen() to open the file and ‘r’ argument to Read the file which is opened.

<?php
$file = fopen("mistonline.in.txt", "r") or exit("Unable to open file!");
//Output of the text file
while(!feof($file))
  {
//<br> is used to break b/w lines
 echo fgets($file). "<br />";
  }
fclose($file);
?>

fclose() as the name says will close the file once the read operation is done. It is always a best practice to close the file once data is read.

5 thoughts on “Read file using php”

Leave a Reply

Your email address will not be published. Required fields are marked *