We will use some file operations to read a large file line by line and display it.
- Read a file: We will read the file by using fopen() function. This function is used to read and open a file.
Syntax:
fopen("filename", access_mode);
Parameter:
- 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.
- 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.
Syntax:
feof($file)
Parameter:- $file: It is the file name
Get the data line by line: We can get the data line by line by using fgets() method.
Syntax:
fgets($file)
Parameter:
- $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.
PHP
<?php // 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.
Output: