ZetCode

PHP file Function

last modified April 3, 2025

The PHP file function reads an entire file into an array. Each element in the array corresponds to a line in the file. This is useful for processing text files line by line.

Basic Definition

The file function reads a file and returns its contents as an array of lines. It takes three parameters: filename, flags, and context.

Syntax: file(string $filename, int $flags = 0, resource $context = null): array|false. The function returns false on failure.

Basic file Example

This shows the simplest usage of file to read a file into an array.

basic_file.php
<?php

declare(strict_types=1);

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

foreach ($lines as $line) {
    echo $line;
}

This reads 'data.txt' into an array where each element is a line from the file. The newline characters are included at the end of each line.

Reading Without Newlines

Using the FILE_IGNORE_NEW_LINES flag removes newline characters from each line.

no_newlines.php
<?php

declare(strict_types=1);

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

foreach ($lines as $line) {
    echo "Line: $line\n";
}

The FILE_IGNORE_NEW_LINES flag makes each array element contain the line content without the terminating newline character. This is often more convenient.

Skipping Empty Lines

The FILE_SKIP_EMPTY_LINES flag skips empty lines in the input file.

skip_empty.php
<?php

declare(strict_types=1);

$lines = file('data.txt', FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES);

foreach ($lines as $line) {
    echo "Line: $line\n";
}

This example combines two flags to skip empty lines and remove newlines. The bitwise OR operator (|) is used to combine multiple flags.

Reading Remote Files

The file function can also read from URLs when allow_url_fopen is enabled.

remote_file.php
<?php

declare(strict_types=1);

$lines = file('https://example.com/data.txt', FILE_IGNORE_NEW_LINES);

if ($lines !== false) {
    foreach ($lines as $line) {
        echo "Line: $line\n";
    }
} else {
    echo "Failed to read file";
}

This reads a file from a remote server. Always check the return value for false when reading remote files as the operation might fail.

Error Handling

Proper error handling is important when working with files.

error_handling.php
<?php

declare(strict_types=1);

$filename = 'nonexistent.txt';
$lines = file($filename);

if ($lines === false) {
    echo "Error: Could not read file '$filename'";
    exit(1);
}

foreach ($lines as $line) {
    echo $line;
}

This example demonstrates proper error handling. The function returns false on failure, which we check before processing the results.

Best Practices

Source

PHP file Documentation

This tutorial covered the PHP file function with practical examples showing its usage in different scenarios.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all PHP Filesystem Functions.