file() Function is one of the PHP operations used in filesystem and stream workflows. Reads a file into an array of lines. The examples below show what it does and how to handle its result.
What Is file() Function?
Reads a file into an array of lines.
Its syntax is:
file(string $filename, int $flags = 0, $context = null)
The parameters are:
string $filename— The source or destination filesystem path.int $flags = 0— Option constants that alter how the operation behaves.$context = null— Optional PHP stream context controlling relevant stream or wrapper behavior.
Return value: The declared return type is array|false. Large files are loaded entirely into memory; use fgets() for streaming.
Important: Large files are loaded entirely into memory; use fgets() for streaming.
Example 1: Basic Usage
The basic example focuses on the behavior of file() Function: reads a file into an array of lines. Inputs are defined locally or shown as application-controlled paths.
<?php
$lines = file(__FILE__, FILE_IGNORE_NEW_LINES);
echo count($lines);
The displayed value, file content or return status depends on the input and surrounding environment. Results involving local paths, identifiers or generated files are not fixed values.
Example 2: A Different Use Case
This second example illustrates another input or execution condition for file() Function. Notice the different checks, arguments, or output handling.
<?php
$lines = file(__FILE__, FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES);
if ($lines !== false) { echo $lines[0] ?? "Empty file"; }
This variation matters because large files are loaded entirely into memory; use fgets() for streaming.
Practical Example: Using the Operation in an Application
The following example demonstrates how file() Function fits into a more complete file or archive workflow, including inputs and appropriate checks.
<?php
$items = file(__DIR__ . "/keywords.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if ($items === false) { throw new RuntimeException("File read failed"); }
$items = array_map("trim", $items);
print_r($items);
The operation shown is an illustrative script. Adapt the filenames, required permissions, and error handling to your own controlled application environment before running it.
Important Behavior and Edge Cases
- Function behavior: Reads a file into an array of lines.
- Limitation: Large files are loaded entirely into memory; use fgets() for streaming.
Comparison With Related Functions
| Function or Method | Difference |
|---|---|
file() Function | Reads a file into an array of lines. |
fgets() | Reads one line at a time; file() constructs an array of all lines. |
Use file() Function for the specific behavior described above. The related operation, fgets(), serves a different purpose; do not substitute it unless that different behavior is intended.
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Assuming this operation has the same behavior on every file or environment. | Large files are loaded entirely into memory; use fgets() for streaming. Check prerequisites and return values before using the result. |
| Treating fgets() as an interchangeable replacement for file() Function. | Reads one line at a time; file() constructs an array of all lines. |
Key Takeaways
- Reads a file into an array of lines.
- Large files are loaded entirely into memory; use fgets() for streaming.
- Related operation: fgets() — Reads one line at a time; file() constructs an array of all lines.
file() Function is useful when its specific behavior matches your application requirement. Keep paths controlled, check failures, and handle environment-dependent operations deliberately.