PHP: How Do I Read From a Text File?

by on January 25, 2006 · 14 comments· LAST UPDATED June 25, 2012

in , ,

How do I read from text file using PHP under Linux or UNIX?

PHP does supports the file reading and writing. You can open a file using the fopen() function. Once the file is open, you can use the fgets() function to read from the file.

For example, if you wish to open file called /tmp/file1.txt then following PHP code will help you to read file. fopen() will open fine in r (read) mode. Then while loop will open file using fgets() function till end of file:

php source code

<title>File read demo</title>
$file = "/tmp/file1.txt";
$f = fopen($file, "r");
while ( $line = fgets($f, 1000) ) {
print $line;

See fopen() and fgets() for more information.

Tweet itFacebook itG+ itDownload PDF versionFound an error/typo on this page?

{ 14 comments… read them below or add one }

1 Chidambaram February 23, 2009 at 11:12 am

Thanks for the provide the coding ..
I need a another one help ,( unknow size of the text file ) using for loop to retrieve the textf ile value…


2 Balaviswanathan.V April 7, 2009 at 7:17 am

Thanks a lot Vivek for providing necessary information and also would like to know that
Is it possible to execute perl script inside php?


3 vimal mishra October 1, 2009 at 7:29 am

very good code


4 mc February 7, 2010 at 8:53 am

$str = file_get_contents(“file.txt”);


5 Mukesh Dak June 17, 2010 at 1:27 pm

Really simplest way to read a file.


6 Chals March 18, 2011 at 6:25 am

if i read this file and print, than
-new line char, tabs, gets lost

is it possible to print text file as same as its in original format !

please suggest


7 Warkanum April 27, 2011 at 8:24 am

This works for me:

if ($_FILES['fileUpload']['error'] == UPLOAD_ERR_OK
      && is_uploaded_file($_FILES['fileUpload']['tmp_name'])) {
  echo file_get_contents($_FILES['fileUpload']['tmp_name']);

Hope it helps.

Edited by admin: added pre tags.


8 Warkanum April 27, 2011 at 8:25 am

Well, use the pre tags to display the variable with file contents with print. Obove example posted a bit weird.


9 Mukesh Soni October 1, 2011 at 2:56 pm

Very very good cods & thanks for making this code


10 sh November 30, 2011 at 5:18 am

it prints only the last line from the text we need to use any foreach loop?


11 lucky January 3, 2012 at 8:24 pm

hi, i want to ask a question that . it i have a test.txt file with this data .

Student Table.
1- student 1
2- student 2
3- student 3

this file located in D:\test.txt …..
how can i read this file on web page and show out put like first line as title and other lines are same as it is.

plz help. sorry for bad english.
Thanx in advance


12 M. Muneer January 31, 2012 at 10:10 am

thanks its very use full code for me


13 King Musa July 16, 2012 at 8:27 am

@sh I don’t knw why it does that from your side but to me this cript does exactly the real job : what I dont undestand is that 1000
: This is gud thanks


14 Anov Siradj August 6, 2013 at 7:14 pm

Thanks, it’s very helpfull…, :)


Leave a Comment

Tagged as: , , , , , ,

Previous Faq:

Next Faq: