Backend Development 5 min read

Using PHP file() Function to Read Files into an Array

This article explains how the PHP file() function reads a file’s contents into an array, describes its parameters, demonstrates basic usage and advanced options such as ignoring newline characters, and provides complete code examples for practical implementation.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP file() Function to Read Files into an Array

In PHP development, it is often necessary to read the contents of a file for processing. PHP provides a range of file‑handling functions, and one of the most commonly used is the file() function. The file() function reads the contents of a file into an array, making it easy to manipulate and operate on the data.

The syntax of the file() function is as follows:

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

Parameters:

$filename : the name of the file to read, which can be an absolute or relative path.

$flags : optional flags that control how the file is read.

$context : optional resource that specifies the file’s context.

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

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

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

In this example, the file() function reads the contents of data.txt into the array $fileContent . Then a foreach loop iterates over the array, outputting each line to the browser, allowing the entire file to be read in one operation.

When reading file contents, file() automatically treats each line as a separate array element, with each element containing the full line. By looping through the array, you can output the file line by line.

Note that file() reads the file line by line by default and automatically strips the trailing newline character from each line.

Beyond simply reading a file into an array, file() can accept additional parameters to specify special reading operations. For example, the second parameter $flags can be set to FILE_IGNORE_NEW_LINES to ignore the newline character at the end of each line.

Here is an example showing how to use file() with special options:

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

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

This example reads data.txt into $fileContent while using the FILE_IGNORE_NEW_LINES flag to omit trailing newlines.

In summary, the file() function is a very useful file‑handling utility that conveniently reads a file’s contents into an array. It simplifies processing and manipulation of file data, improving development efficiency.

backend developmentPHParrayCode Examplefile handlingfile function
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.