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.
<?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.
<?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.
<?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.
<?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.
<?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
- 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.
Source
This tutorial covered the PHP file function with practical
examples showing its usage in different scenarios.