Open In App
Related Articles

How to read a Large File Line by Line in PHP ?

Like Article
Save Article
Report issue

We will use some file operations to read a large file line by line and display it.

  1. Read a file: We will read the file by using fopen() function. This function is used to read and open a file.


     fopen("filename", access_mode);
    • filename: Filename is the name of the file
    • access_mode: It is the mode of the file that includes r – read mode and w- write mode.
  2. Traverse through the end of the file: We can traverse by using feof() function. This function is used to traverse till the end of the file.



    • $file: It is the file name
  3. Get the data line by line: We can get the data line by line by using fgets() method.


    • $file: It is the file name

Example: Let us consider the file with data stored in “myfile.txt”. The following is the PHP code to read the file line by line and display.


    // Open your file in read mode
    $input = fopen("myfile.txt", "r");
    // Display a line of the file until the end 
    while(!feof($input)) {
        // Display each line
        echo fgets($input). "<br>";


myfile.txt: The content for the “myfile.txt” is as follows:

Python is a high-level, general-purpose and a very popular programming language. Python programming language (latest Python 3) is being used in web development, Machine Learning applications, along with all cutting edge technology in Software Industry. Python Programming Language is very well suited for Beginners, also for experienced programmers with other programming languages like C++ and Java. Machine Learning is the field of study that gives computers the capability to learn without being explicitly programmed. ML is one of the most exciting technologies that one would have ever come across. As it is evident from the name, it gives the computer that makes it more similar to humans: The ability to learn. Machine learning is actively being used today, perhaps in many more places than one would expect.


file line by line

Last Updated : 14 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Similar Reads