PHP: How do I read from text file?

by Vivek Gite on January 25, 2006 · 11 comments

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

A. 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

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

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

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

{ 11 comments… read them below or add one }

1 Chidambaram February 23, 2009

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…

Reply

2 Balaviswanathan.V April 7, 2009

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

Reply

3 vimal mishra October 1, 2009

very good code

Reply

4 mc February 7, 2010

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

Reply

5 Mukesh Dak June 17, 2010

Really simplest way to read a file.

Reply

6 Chals March 18, 2011

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

Reply

7 Warkanum April 27, 2011

This works for me:

if ($_FILES['fileUpload']['error'] == UPLOAD_ERR_OK
      && is_uploaded_file($_FILES['fileUpload']['tmp_name'])) {
  echo("<pre>");
  echo file_get_contents($_FILES['fileUpload']['tmp_name']);
  echo("</pregt;");

Hope it helps.

Edited by admin: added pre tags.

Reply

8 Warkanum April 27, 2011

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

Reply

9 Mukesh Soni October 1, 2011

Very very good cods & thanks for making this code

Reply

10 sh November 30, 2011

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

Reply

11 lucky January 3, 2012

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

—————-test.txt——————–
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

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 11 + 10 ?
Please leave these two fields as-is:
Solve the simple math so we know that you are a human and not a bot.



Previous post:

Next post: