You can use the file () function to read files within a few megabytes into an array by row. You can use array_pop to get the last row. However, for large text files, the machine memory is not large enough, or php itself has a memory_limit limit, this method is not applicable, even if the force is not limited, the efficiency is very low. No way
You can use the file () function to read files within a few megabytes into an array by row. You can use array_pop to get the last row. However, for large text files, the machine memory is not large enough, or php itself has a memory_limit limit, this method is not applicable, even if the force is not limited, the efficiency is very low. No way
You can use the file () function to read files within a few megabytes into an array by row. You can use array_pop to get the last row.
However, for large text files, the machine memory is not large enough, or php itself has a memory_limit limit, this method is not applicable, even if the force is not limited, the efficiency is very low.
No way? Of course there are, but there are no ready-made functions, you need to do it yourself.
Here we need to use the file pointer. If I have learned C, I should know the pointer. In other words, I open a file through fopen in PHP. At this time, the file has not been read, at this time, it points to the beginning of the file, and the pointer position is 0. When you read the content from the file through fgets or fgetc, the pointer also advances accordingly.
While (! Feof ($ fp )){
$ Data. = fgets ($ fp, 4096 );
}
The implementation principle is that fgets reads the specified length string from the position of the current pointer until a line break is encountered.
Can I control the pointer position to the nth row at the bottom? Unfortunately, no, but the pointer can be directly moved to the end, and N positions can be regressed through the fseek () function.
First, we move the pointer to the end, and move backward to two positions. We read a character through fgetc and determine whether the character is "\ n" or a line break. If it is not a line break, then, continue to return to a position and judge again until we go back to the end of the last line, and use fgets to retrieve all rows. Two while loops are used here. The outer loop controls the number of rows to be obtained, and the inner loop controls the fseek action.
/*** Get the last $ n line of the file * @ param string $ filename file path * @ param int $ n the last few lines * @ return mixed false indicates an error occurred, returns the string */function FileLastLines ($ filename, $ n) {if (! $ Fp = fopen ($ filename, 'R') {echo "failed to open the file. Please check whether the file path is correct and the path and file name do not contain Chinese characters"; return false ;} $ pos =-2; $ eof = ""; $ str = ""; while ($ n> 0) {while ($ eof! = "\ N") {if (! Fseek ($ fp, $ pos, SEEK_END) {$ eof = fgetc ($ fp); $ pos --;} else {break;} $ str. = fgets ($ fp); $ eof = ""; $ n --;} return $ str;} echo nl2br(FileLastLines('sss.txt ', 4 ));