Read a file backwards line by line using fseek

后端 未结 9 2039
温柔的废话
温柔的废话 2020-11-27 07:33

How do I read a file backwards line by line using fseek?

code can be helpful. must be cross platform and pure php.

many thanks in advance

regards

9条回答
  •  一向
    一向 (楼主)
    2020-11-27 08:21

    If you are going to read the entire file in anyways, just use file() to read the file in as an array (each line is each element in the array) and then use array_reverse() to flip the array backwards and loop through that. Or just do a reverse for loop where you start at the end and decrement on each loop.

    $file = file("test.txt");
    $file = array_reverse($file);
    foreach($file as $f){
        echo $f."
    "; }

提交回复
热议问题