Last updated on August 8th, 2022 at 10:06 am

In this tutorial we will read the text file from bottom to top using PHP. There is array_reverse function available in PHP we are utilizing that along with the regular file function to read the file. Once each line is read using foreach statement we can assign those lines to a variable named $dis_line and print it.

This is how my text.txt file looks like

1 John Deo Four 75
2 Max Ruin Three 85
3 Arnold Three 55
4 Krish Star Four 60
5 John Mike Four 60
6 Alex John Four 55

Now let us create a PHP file with any name and copy the code below. Save it and run.

$file = file('text.txt');
$read_rev = array_reverse($file);
foreach ($read_rev as $dis_line) {
echo "<br />";

In the browser you will see the PHP code has reversed the content of file (text.txt) by displaying last line of the file first followed by second last line etc., Check out the DEMO for more insight.


Leave a Reply

Your email address will not be published. Required fields are marked *