W3docs

file()

The file() function is a built-in PHP function that reads the contents of a file and stores them in an array. This function is often used when we need to access

What is the file() Function?

The file() function reads an entire file into an array, where each element of the array is one line of the file. It is the quickest way to get a file's lines into a PHP array in a single call — you do not need to open a handle, loop with fgets(), or close anything afterwards.

Reach for file() when you want to process a file line by line: counting lines, filtering rows, reading a list of values, or parsing a small log. For one big string instead of an array, use file_get_contents(); to write a file in one call, use file_put_contents().

Syntax

file(string $filename, int $flags = 0, ?resource $context = null): array|false
ParameterDescription
$filenamePath to the file to read. Can be a local path or a URL (if allow_url_fopen is enabled).
$flagsOptional. One or more flags, combined with the bitwise OR operator (|). See the table below.
$contextOptional. A stream context resource created with stream_context_create().

Return value: an array of lines, or false on failure (for example, if the file does not exist). By default each line keeps its trailing newline (\n).

The flags Parameter

$flags lets you control how lines are read. Combine them with |:

FlagEffect
FILE_IGNORE_NEW_LINESStrip the newline character from the end of each line.
FILE_SKIP_EMPTY_LINESSkip empty lines (most useful together with FILE_IGNORE_NEW_LINES).
FILE_USE_INCLUDE_PATHSearch for the file in the include_path too.
<?php
// Lines without trailing "\n", and no blank lines.
$lines = file('myfile.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

How to Use the file() Function

A self-contained example that creates a file, reads it back with file(), and loops over the lines:

<?php

// Create a small file to read.
file_put_contents('myfile.txt', "First line\nSecond line\nThird line\n");

$lines = file('myfile.txt');

foreach ($lines as $lineNumber => $line) {
    echo "Line #{$lineNumber}: " . trim($line) . "\n";
}

This prints:

Line #0: First line
Line #1: Second line
Line #2: Third line

The array keys are zero-based line numbers, and the values are the line contents. We use trim() here to drop the trailing newline that file() keeps by default — passing FILE_IGNORE_NEW_LINES would do the same at read time.

Counting Lines in a File

Because file() returns an array, count() gives you the line count directly:

<?php

file_put_contents('myfile.txt', "alpha\nbeta\ngamma\n");

$lines = file('myfile.txt', FILE_IGNORE_NEW_LINES);

echo "The file has " . count($lines) . " lines.\n";  // The file has 3 lines.

Handling Errors

When the file cannot be read, file() returns false and emits a warning. Always check the result before looping:

<?php

$lines = @file('does-not-exist.txt');

if ($lines === false) {
    echo "Could not read the file.\n";
} else {
    echo "Read " . count($lines) . " lines.\n";
}

The @ operator suppresses the built-in warning so you can handle the failure yourself.

Gotchas

  • Whole file in memory. file() loads every line into an array at once. For very large files, open a handle with fopen() and read line by line with fgets() instead.
  • Trailing newlines are kept unless you pass FILE_IGNORE_NEW_LINES. This trips up exact string comparisons, e.g. $lines[0] === 'value' fails when the element is actually "value\n".
  • Check existence first if you want to avoid warnings: pair it with file_exists() or suppress with @.

Conclusion

The file() function is the simplest way to read a file into an array of lines in PHP. Use the FILE_IGNORE_NEW_LINES and FILE_SKIP_EMPTY_LINES flags to get clean lines, always guard against a false return value, and prefer fgets() for files too large to hold in memory.

Practice

Practice
What functions are used in PHP to read and write to a file?
What functions are used in PHP to read and write to a file?
Was this page helpful?