PHP: How Do I Read From a Text File?

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.

ADVERTISEMENTS

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.

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
15 comments… add one
  • Chidambaram Feb 23, 2009 @ 11:12

    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…

  • Balaviswanathan.V Apr 7, 2009 @ 7:17

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

  • vimal mishra Oct 1, 2009 @ 7:29

    very good code

  • mc Feb 7, 2010 @ 8:53

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

    • Mukesh Dak Jun 17, 2010 @ 13:27

      Really simplest way to read a file.

  • Chals Mar 18, 2011 @ 6:25

    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

  • Warkanum Apr 27, 2011 @ 8:24

    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.

    • Warkanum Apr 27, 2011 @ 8:25

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

  • Mukesh Soni Oct 1, 2011 @ 14:56

    Very very good cods & thanks for making this code

  • sh Nov 30, 2011 @ 5:18

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

  • lucky Jan 3, 2012 @ 20:24

    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

  • M. Muneer Jan 31, 2012 @ 10:10

    Hi,
    thanks its very use full code for me

  • King Musa Jul 16, 2012 @ 8:27

    @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

  • Anov Siradj Aug 6, 2013 @ 19:14

    Thanks, it’s very helpfull…, :)

  • Arup Ghosh Jun 2, 2015 @ 18:13

    Thanks for the code. Can you please elaborate how can we handle a specific column or row of text in php?

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.