Home >Backend Development >PHP Problem >How to use php file function
php file() function is used to read the entire file into an array, the syntax is file(path,include_path,context)
. Each element in the array is a corresponding line in the file, including newlines.
How to use the php file() function?
The file() function reads the entire file into an array.
Syntax:
file(path,include_path,context)
Parameters:
●path: required. Specifies the file to be read.
●include_path: optional. Set this parameter to '1' if you also want to search for files in include_path (in php.ini).
● context: optional. Specifies the environment for a file handle. context is a set of options that can modify the behavior of the stream. If NULL is used, it is ignored.
Note: Each row in the returned array includes line terminators, so if line terminators are not required, you need to use the rtrim() function.
Tip: As of PHP 4.3, this function is binary safe. (Meaning that both binary data (such as images) and character data can be written using this function.)
php file() function example
<?php print_r(file("test.txt")); ?>
Output:
Array ( [0] => Hello World. Testing testing! [1] => Another day, another line. [2] => If the array picks up this line, [3] => then is it a pickup line? )
The above is the detailed content of How to use php file function. For more information, please follow other related articles on the PHP Chinese website!