Here is the simple trick to read the text file line by line. For this I am going to use the [code]fopen()[/code] and [code]fgets()[/code] function.
This is code snippet I am going to take one text file and read it line by line.
Let’s for the code:
[cc lang=”php”]
$file = “/path/to/the/file”;
$fp = fopen($file, “r”);
while ( $line = fgets($fp, 1000) )
{
echo $line;
}
[/cc]
Instead, us this: http://php.net/file_get_contents
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.
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!