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
<html> <head> <title>File read demo</title> </head> <body> <?php $file = "/tmp/file1.txt"; $f = fopen($file, "r"); while ( $line = fgets($f, 1000) ) { print $line; } ?> </body> </html>
See fopen() and fgets() for more information.
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop










{ 13 comments… read them below or add one }
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…
Thanks a lot Vivek for providing necessary information and also would like to know that
Is it possible to execute perl script inside php?
very good code
$str = file_get_contents(“file.txt”);
Really simplest way to read a file.
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
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.
Well, use the pre tags to display the variable with file contents with print. Obove example posted a bit weird.
Very very good cods & thanks for making this code
it prints only the last line from the text file..do we need to use any foreach loop?
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
Hi,
thanks its very use full code for me
@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