Home PHP Filesystem file() PHP file() Function 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.
$lines = file('data.txt', FILE_IGNORE_NEW_LINES); Call What 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 file(string $filename, int $flags = 0, ?resource $context = null): array|false Parameters Parameter Description filename Required. The path to the file you want to read. URLs also work when allow_url_fopen is on. flags Optional. One or more of FILE_USE_INCLUDE_PATH, FILE_IGNORE_NEW_LINES and FILE_SKIP_EMPTY_LINES, combined with the | operator. context Optional. A stream context created with stream_context_create(). Use null for the default.
Return Value Return Value Description array An array in which each item is one line of the file. false Returned when the file cannot be read, for example because it does not exist.
Important
Without FILE_IGNORE_NEW_LINES every line keeps its trailing newline, which often surprises beginners when they compare values. Also check for false before using the result, since a missing file returns false and raises a warning.
PHP Examples 1. Load a File into an Array 2. Handle a Missing File 3. Skip Empty Lines 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
$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); Lines: 3
1: Red
2: Green
3: Blue How It Works file_put_contents() creates a file with one color per line. file() reads the whole file and returns an array with three items. FILE_IGNORE_NEW_LINES removes the line break from the end of each item. 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
$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 The path points to a file that does not exist. file() cannot read it, so it returns false (and normally raises a warning). The @ operator hides the warning, so the script can handle the problem itself. The if statement checks for false and prints a friendly message. Frequently Asked Questions What does file() do in PHP? 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.
How do I remove the newline characters from file()? Pass FILE_IGNORE_NEW_LINES as the second argument. Without it, every line keeps its line break.
What is the difference between file() and file_get_contents()? file() returns an array of lines, while file_get_contents() returns the whole file as one string.
Is file() safe for large files? No. It loads the entire file into memory. For big files, read line by line with fopen() and fgets() instead.
Why does file() return false? 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 Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion