Read file into array
Read file content: file()
If you want to be able to read a file, you can use the file()
function. It puts the file contents into an array so you can work with it line by line or as a list.
Assuming you have a list of countries in a text (.txt)
file and you need to display the list, you can use the file()
function to read the file and convert it to an array.
Hereβs an example:
The text file content:
In the example above, the output would be:
The file()
function parameters
The file()
function has three parameters which are passed as arguments.
$filename
: is the path to the file.$flags
: is an optional parameter that can be one or more of the constants.$context
: allows you to specify a context for the file handle.
Below are the file constants:
Flag | Description |
---|---|
FILE_USE_INCLUDE_PATH | Search for the file in the include path. |
FILE_IGNORE_NEW_LINES | Skip the newline at the end of the array element. |
FILE_SKIP_EMPTY_LINES | Skip empty lines in the file. |
Adding options and context
File types that can be handled
Various file types can be handled by the file()
function, including:
Text files (*.txt)
: These are standard text files that contain plain, unformatted text.CSV files (*.csv)
: These are comma-separated values files that contain tabular data.HTML files (*.html, *.htm)
: These files contain HTML markup and can be read into an array.XML files (*.xml)
: These are files that contain structured data in the XML format.Log files (*.log)
: These are text-based files used to store a record of events or messages.Config files (*.config, *.ini, etc.)
: These are files that store configuration data in a specific format.
File extensions are not a restriction on the file()
function. In general it can read any text-based file. The result might not be as expected if the file contains binary data or non-textual data.