PHP file function tutorial shows how to read files into arrays in PHP. Learn file function with practical examples.
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.
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.
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.
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.
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.
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.
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.
Error Checking: Always check if the function returned false.
Memory Usage: For large files, consider fgets() instead.
Security: Validate file paths when using user input.
Flags: Use appropriate flags for your use case.
Permissions: Ensure proper file permissions.
This tutorial covered the PHP file function with practical examples showing its usage in different scenarios.
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.