Search code examples
phptail

Issue with returning the last 10 lines of a file in PHP


Currently, I have this code to return the last 10 lines in the file: food.txt

<?php
echo `tail /home/food.txt`;

and the output returns something like:

pie apple orange fish lemon peach egg bacon pancake strawberry

The problem is, running the tail command in PHP is ignoring the lines originally in the file food.txt, if I run the same command over SSH, I get:

pie 
apple 
orange 
fish 
lemon 
peach 
egg 
bacon 
pancake 
strawberry

I cannot use PHP to open the file (from my knowledge) because the file will grow pretty large over time and I'm not sure PHP will be able to handle it, and by large, I mean 250,000+ lines.

Thanks.


Solution

  • If the problem is missing line breaks (not clear from the question) wrap everything in <pre>.

    echo '<pre>';
    echo `tail /home/food.txt`;
    echo '</pre>';
    

    Edited to add: <pre> will give you a monospace font by default; you can fix that easily with CSS if it's a problem.