Backend Development 4 min read

Using PHP's file() Function to Read File Contents into an Array

This article explains PHP's file() function, detailing its syntax, parameters, and usage examples for reading a file into an array, including how to handle flags such as FILE_IGNORE_NEW_LINES to control line endings.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP's file() Function to Read File Contents into an Array

In PHP development, it is often necessary to read the contents of a file for further processing. PHP provides a set of file handling functions, among which the file() function is widely used.

The file() function reads the entire file into an array, making it convenient to manipulate and operate on each line of the file.

Syntax of the file() function is as follows:

array file ( string $filename [, int $flags = 0 [, resource $context ]] )

Parameters:

$filename : The path to the file to be read (absolute or relative).

$flags (optional): Flags that modify the way the file is read.

$context (optional): A context resource for the file operation.

Below is a concrete example demonstrating how to use file() to read a file into an array:

<?php
// Read file contents into an array
$fileContent = file('data.txt');

// Output each line
foreach ($fileContent as $line) {
    echo $line . "<br>";
}
?>

In this example, file() reads data.txt and stores each line as an element of $fileContent . The foreach loop then iterates over the array and outputs each line to the browser, effectively displaying the whole file.

When reading a file, file() automatically treats each line as a separate array element and, by default, removes the trailing newline character from each line.

Beyond simply reading a file, file() can accept additional flags to control its behavior. For instance, using the FILE_IGNORE_NEW_LINES flag tells the function to keep the newline characters at the end of each line.

Example with the FILE_IGNORE_NEW_LINES flag:

<?php
// Read file contents into an array while ignoring newline characters
$fileContent = file('data.txt', FILE_IGNORE_NEW_LINES);

// Output each line
foreach ($fileContent as $line) {
    echo $line . "<br>";
}
?>

This snippet reads data.txt into $fileContent while preserving the original line endings, thanks to the FILE_IGNORE_NEW_LINES flag.

In summary, the file() function is a highly practical tool for file handling in PHP, allowing developers to quickly load an entire file into an array and manipulate its contents efficiently, thereby improving development productivity.

Backendphparraysfile handlingfile-reading
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.