1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

question about reading files in php

Discussion in 'PHP & Perl' started by cyberps, Sep 9, 2015.

  1. cyberps

    cyberps Newbie

    Joined:
    Jul 15, 2015
    Messages:
    4
    Likes Received:
    0
    Hey folks,

    I tried to use fgets() to read a file line by line using this code..
    Code:
    while(!feof($txt)){
    echo fgets($txt) . "<br>";
    }
    fclose($txt);
    its read the file from the first line till the last one

    but I wanna read the file from the last line till the first one
    how can I do that please ?
     
  2. MrBlue

    MrBlue Senior Member

    Joined:
    Dec 18, 2009
    Messages:
    974
    Likes Received:
    680
    Occupation:
    Web/Bot Developer
    Try reading in the contents of the file, line by line, into an array then reversing the order.

    Code:
    <?php
    $lines = file('filename.txt', FILE_IGNORE_NEW_LINES);
    $reversed = array_reverse($lines);
    ?>
    
     
    • Thanks Thanks x 1
  3. cyberps

    cyberps Newbie

    Joined:
    Jul 15, 2015
    Messages:
    4
    Likes Received:
    0
    Thanks bro :) its work
     
  4. revproxy

    revproxy BANNED BANNED Jr. VIP Premium Member

    Joined:
    Nov 20, 2015
    Messages:
    396
    Likes Received:
    101
    Gender:
    Male
    an options to:
    PHP:
    $filestring file_get_contents($filename);
    $filearray explode("\n"$filestring);