Reading data from a PHP text file

I'm just wondering how I can read a text file in php, I would like it to display the last 200 records (each of them in a new line) from a text file.

how

John white
Jane does
John does
Someones name

etc.

Thank!

+3
source share
5 answers

Use fopen and fgets , or maybe just file .

+5
source

There are several ways to read text from files in PHP.

fgets, fread .. , 200 .

+1

file will get the contents of the file and put it in an array. After that, as Jalton said, output the last 200 elements.

+1
source

This displays the last 200 lines. The last line is the first:

$lines = file("filename.txt");
$top200 = array_slice(array_reverse($lines),0,200);
foreach($top200 as $line)
{
    echo $line . "<br />";
}
0
source
<?php
$myfile = fopen("file_name.txt", "r") or die("Unable to open file!");
// Output one character until end-of-file
while(!feof($myfile)) {
   echo fgetc($myfile);
}

fclose($myfile);
?>

You can use this

0
source

Source: https://habr.com/ru/post/1741343/


All Articles