PHP file() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, file() reads a whole file and returns it as an array, with one item for each line. It is the quickest way to load a small text file into a list.

You do not need fopen(), fgets() or fclose(). Give file() a filename and it does the opening, reading and closing for you, then hands back an array that you can loop over with foreach.

Syntax

Call file() with the values you need. Start with this line.

PHP
$lines = file('data.txt', FILE_IGNORE_NEW_LINES);
CallWhat it does
file('data.txt')Returns an array of lines, each one still ending with a line break.
file('data.txt', FILE_IGNORE_NEW_LINES)Returns the lines with the line breaks removed.
file('data.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES)Returns the lines without line breaks and without empty lines.

file() loads the entire file into memory. It is perfect for small files, but for very large files read line by line with fgets(). FILE_SKIP_EMPTY_LINES only works together with FILE_IGNORE_NEW_LINES.

Library declarations
PHP
file(string $filename, int $flags = 0, ?resource $context = null): array|false

Parameters

ParameterDescription
filenameRequired. The path to the file you want to read. URLs also work when allow_url_fopen is on.
flagsOptional. One or more of FILE_USE_INCLUDE_PATH, FILE_IGNORE_NEW_LINES and FILE_SKIP_EMPTY_LINES, combined with the | operator.
contextOptional. A stream context created with stream_context_create(). Use null for the default.

Return Value

Return ValueDescription
arrayAn array in which each item is one line of the file.
falseReturned when the file cannot be read, for example because it does not exist.

PHP Examples

Example 1 — Load a File into an Array

Create a file with three colors and read it into an array without the line breaks.

PHP
<?php
$path = sys_get_temp_dir() . '/codetofun_file.txt';
file_put_contents($path, 'Red' . PHP_EOL . 'Green' . PHP_EOL . 'Blue' . PHP_EOL);

$lines = file($path, FILE_IGNORE_NEW_LINES);

echo 'Lines: ', count($lines), PHP_EOL;

foreach ($lines as $i => $line) {
    echo $i + 1, ': ', $line, PHP_EOL;
}

unlink($path);

How It Works

  1. file_put_contents() creates a file with one color per line.
  2. file() reads the whole file and returns an array with three items.
  3. FILE_IGNORE_NEW_LINES removes the line break from the end of each item.
  4. foreach prints each line, and $i + 1 turns the zero-based index into a line number.

Example 2 — Handle a Missing File

When the file does not exist, file() returns false and raises a warning. Check the result before you use it.

PHP
<?php
$path = sys_get_temp_dir() . '/codetofun_missing_file.txt';

$lines = @file($path);

if ($lines === false) {
    echo 'Could not read the file', PHP_EOL;
} else {
    echo 'Lines: ', count($lines), PHP_EOL;
}

How It Works

  1. The path points to a file that does not exist.
  2. file() cannot read it, so it returns false (and normally raises a warning).
  3. The @ operator hides the warning, so the script can handle the problem itself.
  4. The if statement checks for false and prints a friendly message.

Example 3 — Skip Empty Lines

Combine two flags to remove both the line breaks and any empty lines from the result.

PHP
<?php
$path = sys_get_temp_dir() . '/codetofun_file_skip.txt';
file_put_contents($path, 'A' . PHP_EOL . PHP_EOL . 'B' . PHP_EOL);

$lines = file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

echo 'Lines: ', count($lines), PHP_EOL;
echo implode(', ', $lines), PHP_EOL;

unlink($path);

How It Works

  1. The file has three lines, and the middle one is empty.
  2. FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES combines both flags with the | operator.
  3. The empty line is dropped, so the array holds only A and B.
  4. implode() joins the two items into one readable string.

Frequently Asked Questions

It reads a whole file and returns an array where each item is one line of the file. It opens and closes the file for you.
Pass FILE_IGNORE_NEW_LINES as the second argument. Without it, every line keeps its line break.
file() returns an array of lines, while file_get_contents() returns the whole file as one string.
No. It loads the entire file into memory. For big files, read line by line with fopen() and fgets() instead.
The file probably does not exist, the path is wrong, or PHP does not have permission to read it. Check with file_exists() and is_readable().

Next: file_exists()

file_exists() checks whether a file or a folder exists at the given path. It returns true .

file_exists() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful