$file="./doc.txt";
$doc=file_get_contents($file);
$line=explode("\n",$doc);
foreach($line as $newline){
echo '<h3 style="color:#453288">'.$newline.'</h3><br>';
}
Try something like this:
$filename = 'file.txt';
$data = file($filename);
foreach ($data as $line_num=>$line)
{
echo 'Line # <b>'.$line_num.'</b>:'.$line.'<br/>';
}
$aa = fopen('a.txt','r');
echo fread($aa,filesize('a.txt'));
$a = fopen('a.txt','r');
while(!feof($a)){echo fgets($a)."<br>";}
fclose($a);
$your_variable = file_get_contents("file_to_read.txt");
You can also produce array by using file:
$array = file('/path/to/text.txt');
You can read a group of txt files in a folder and echo the contents like this.
<?php
$directory = "folder/";
$dir = opendir($directory);
$filenames = [];
while (($file = readdir($dir)) !== false) {
$filename = $directory . $file;
$type = filetype($filename);
if($type !== 'file') continue;
$filenames[] = $filename;
}
closedir($dir);
?>
$filename = "fille.txt";
$fp = fopen($filename, "r");
$content = fread($fp, filesize($filename));
$lines = explode("\n", $content);
fclose($fp);
print_r($lines);
In this code full content of the file is copied to the variable $content
and then split it into an array with each newline character in the file.
http://php.net/manual/en/function.file-get-contents.php
http://php.net/manual/en/function.explode.php
$array = explode("\n", file_get_contents($filename));
This wont actually read it line by line, but it will get you an array which can be used line by line. There are a number of alternatives.
W3Schools is your friend: http://www.w3schools.com/php/func_filesystem_fgets.asp
And here: http://php.net/manual/en/function.fopen.php has more info on fopen including what the modes are.
What W3Schools says:
<?php
$file = fopen("test.txt","r");
while(! feof($file))
{
echo fgets($file). "<br />";
}
fclose($file);
?>
fopen opens the file (in this case test.txt with mode 'r' which means read-only and places the pointer at the beginning of the file)
The while loop tests to check if it's at the end of file (feof) and while it isn't it calls fgets which gets the current line where the pointer is.
Continues doing this until it is the end of file, and then closes the file.
Source: Stackoverflow.com