In this tutorial, we are going to learn how to read file line by line in three different ways in PHP with the help of program examples.
This article introduces PHP methods for reading a file line by line.
Use fgets() Function to Read a Large File Line by Line in PHP
The fgets()
is a built-in method that reads a single line from an open file. This function can be used to read a huge file line by line.
Syntax:
The following is the right syntax for using this function.
fgets( File, LengthOfFile );
Parameters:
The fgets()
function has two parameters. The specifics of its parameters are described below:
File
– Required. It is the file that we want to read line by line.LengthOfFile
– Optional. It specifies the length of the file to be read. Its default value is 1024 bytes.
Return Value:
If successful, this method returns a string of LengthOfFile
-1 from the user-specified file. On failure, False is returned.
Example:
The following code demonstrates how to use the fgets()
method to read a big file line by line.
The fopen()
and fclose()
functions will be used to open and close a file, respectively. php_sample_text
.txt is a text file that contains information about PHP.
<?php if ($file = fopen("php_sample_text.txt", "r")) { while(!feof($file)) { $line = fgets($file); echo ("$line"); } fclose($file); } ?>
We utilized the feof()
function to read the file to completion.
The output displays the file’s contents line by line.
Output:
What is PHP? PHP is an open-source, interpreted and object oriented scripting language that is especially suited for web development and can be embedded in HTML. Why We Use PHP? PHP is a server side scripting language embedded in HTML. PHP is used to manage or create dynamic content, databases, session tracking, and even building e-commerce sites. Advantages of PHP PHP is a recursive acronym for “PHP: Hypertext Preprocessor“. It is integrated with a number of popular databases, including MySQL, PostgreSQL, Oracle, Sybase, Informix and Microsoft SQL Server. PHP is pleasingly zippy in its execution, especially when compiled as an Apache module on the Unix side. The MySQL server once started, executes even very complex queries with huge result sets in recording setting-time. It supports a large number of major protocols such as POP3, IMAP and LDAP. PHP4 added support for java and distributed object architectures (COM and COBRA). PHP is forgiving: PHP language tries to be as forgiving as possible. PHP syntax is C-like. PHP is simple and easy to learn. Important Characteristic’s Of PHP Here are the 5 Important Characteristics of PHP Programming Language: Simplicity Efficiency Security Flexibility Familiarity
Use file() Function to Read a Large File Line by Line in PHP
In PHP, the file()
method can also be used to read a file line by line.
The file()
function reads a file line by line into an array.
Additionally, according to PHP documentation, the file()
function reads an entire file into an array.
Syntax:
The following is the correct syntax for using this function:
file( File, Flags, Context )
Parameters:
The file() function has three parameters. The specifics of its parameters are as described below:
File
– Required. It is the path where our file is stored.Flags
– Optional. It specifies the constants that we can use as flags for this function.Context
– Optional. It is a resource that you can create usingstream_context_create()
function.
Return Values:
The full file as an array, or FALSE if unsuccessful.
Example:
The following is the program that reads the file using the file()
function:
<?php
foreach(file('php_sample_text.txt') as $line) {
echo $line. "<br>";
}
?>
Output:
What is PHP? PHP is an open-source, interpreted and object oriented scripting language that is especially suited for web development and can be embedded in HTML. Why We Use PHP? PHP is a server side scripting language embedded in HTML. PHP is used to manage or create dynamic content, databases, session tracking, and even building e-commerce sites. Advantages of PHP PHP is a recursive acronym for “PHP: Hypertext Preprocessor“. It is integrated with a number of popular databases, including MySQL, PostgreSQL, Oracle, Sybase, Informix and Microsoft SQL Server.
PHP is pleasingly zippy in its execution, especially when compiled as an Apache module on the Unix side. The MySQL server once started, executes even very complex queries with huge result sets in recording setting-time.
It supports a large number of major protocols such as POP3, IMAP and LDAP. PHP4 added support for java and distributed object architectures (COM and COBRA).
PHP is forgiving: PHP language tries to be as forgiving as possible. PHP syntax is C-like. PHP is simple and easy to learn. Important Characteristic’s Of PHP Here are the 5 Important Characteristics of PHP Programming Language: Simplicity Efficiency Security Flexibility Familiarity
Use stream_get_line() Function to Read a Large File Line by Line in PHP
Using the stream_get_line()
function, we can also read a file line by line. This method reads a file until a delimiter is encountered.
Syntax:
The following is the correct syntax for using this function:
stream_get_line( Handle, Length, Ending )
Parameters:
The stream_get_line()
function has three parameters. The specifics of its parameters are described below:
Handle
– Optional. It is the path where our file is stored.Length
– Optional. It specifies the file’s length to be read.Ending
– Optional. It is a string that tells about the delimiter.
Return Value:
Returns a string of up to length bytes from the file stream points to. In the event of an error, return false.
Example:
This is the application that reads the file using the stream_get_line()
function.
<?php
$fp = fopen("php_sample_text
.txt", "r") or
die("Unable to open file!");
//reading first 25 bytes and
//displaying the read content
echo stream_get_line($fp, 1080*1080);
//close the file
fclose($fp);
?>
Output:
What is PHP? PHP is an open-source, interpreted and object oriented scripting language that is especially suited for web development and can be embedded in HTML. Why We Use PHP? PHP is a server side scripting language embedded in HTML.
PHP is used to manage or create dynamic content, databases, session tracking, and even building e-commerce sites. Advantages of PHP PHP is a recursive acronym for “PHP: Hypertext Preprocessor“. It is integrated with a number of popular databases, including MySQL, PostgreSQL, Oracle, Sybase, Informix and Microsoft SQL Server.
PHP is pleasingly zippy in its execution, especially when compiled as an Apache module on the Unix side. The MySQL server once started, executes even very complex queries with huge result sets in recording setting-time. It supports a large number of major protocols such as POP3, IMAP and LDAP.
PHP4 added support for java and distributed object architectures (COM and COBRA). PHP is forgiving: PHP language tries to be as forgiving as possible. PHP syntax is C-like. PHP is simple and easy to learn. Important Characteristic’s Of PHP Here are the 5 Important Characteristics of PHP Programming Language: Simplicity Efficiency Security Flexibility Familiarity
Frequently Asked Questions
Which function is used to read PHP files line by line?
The PHP readfile()
function is a built-in function that reads a file and writes its contents to the output buffer.
The filename is passed to the readfile()
function as a parameter, and if it works, it returns the number of bytes read.
If it doesn’t work, it returns FALSE
and an error
.
Which PHP function reads a single line from a file?
With PHP’s fgets()
function, you can read just one line from a file.
This function needs two arguments, which are explained further down.
- File: tells the computer which files to open.
- Length: This is an optional parameter that says how many bytes should be read from the file.
Does Fgets read line by line?
The fgets()
function reads characters from the current stream position up to and including the first new-line character (\n
), to the end of the stream, or until n-1
characters have been read, whichever comes first.
What is Fgets PHP?
The fgets()
function returns a line from a file that is open.
This function returns false on failure and stops returning on a new line at the specified length or end-of-file, whichever occurs first.
How do I view a file in PHP?
The fread()
function reads from an open file.
The first parameter fread()
indicates the file to read from, while the second defines the maximum number of bytes to read.
Summary
In summary, we talked about two ways to use PHP to read a file line by line.
There are a few more ways to do this, but these three will meet almost all of your needs. Use the file()
function when you don’t care about how much memory you use.
If you want to save memory, use fgets()
with a generator function.
Lastly, if you want to learn more about unlink in PHP, please leave a comment below. We’ll be happy to hear it!