Read Text File using PHP

  • Avinash
  • 3
  • Sep 19, 2011
  • Tips & Tricks

Here is the simple trick to read the text file line by line. For this I am going to use the fopen() and fgets() function.

This is code snippet I am going to take one text file and read it line by line.

Let’s for the code:

$file = "/path/to/the/file"; $fp = fopen($file, "r"); while ( $line = fgets($fp, 1000) ) { echo $line; }
$file = "/path/to/the/file";
$fp = fopen($file, "r");
while ( $line = fgets($fp, 1000) )
{
  echo $line;
}

Related Posts

Written by Avinash

Avinash Zala is leading various projects which deals with the various technology involved with the web. A combination of perfect technical and management skills. Avinash would like to chat with you and convert your imagination into the working system. You can get in touch with him on Facebook and Twitter.

View all posts by:

  • Rajeev Rai
  • Karoly Bujtor

    If you don’t use the second parameter the fgets() will keep reading from the stream until it reaches the end of the line not just the first 1000 characters.

  • http://www.learncomputer.com Svetlana

    Hi, guys! Thank you for a useful info. We have posted an article “PHP: Insert Text Into File at Position”, might be useful to your readers too: http://www.learncomputer.com/php-insert-text-into-file-at-position/ Thanks and good luck!