Example
Read a file into an array:
html
<?php
print_r(file("test.txt"));
?>
Definition and Usage
The file() reads a file into an array.
Each array element contains a line from the file, with the newline character still attached.
Syntax
text
file(filename, flag, context)
Parameter Values
| Parameter | Description |
|---|---|
| filename | Required. Specifies the path to the file to read |
| flag | Optional. Can be one or more of the following constants: FILE_USE_INCLUDE_PATH - Search for the file in the include_path (in php.ini) FILE_IGNORE_NEW_LINES - Skip the newline at the end of each array element FILE_SKIP_EMPTY_LINES - Skip empty lines in the file FILE_NO_DEFAULT_CONTEXT - Do not use the default context |
| context | Optional. Specifies the context of the file handle. Context is a set of options that can modify the behavior of a stream. Can be skipped by using NULL. |
Technical Details
| Return Value: | The entire file in an array, FALSE on failure |
|---|---|
| PHP Version: | 4.0+ |
| Binary Safe: | Yes |
| Changelog: | PHP 8.3: Throws a ValuError if flag is an invalid value |