PHP file() funktionen

Definition och användning

file() funktionen läser hela filen in i en array.

Och file_get_contents() Liknande, men skillnaden är att file() returnerar filen som en array. Varje element i arrayen är en rad i filen, inklusive nyckelraden.

Om misslyckas, returnera false.

syntax

file(path,include_path,context)
parameters description
path Required. Specifies the file to be read.
include_path Optional. If you also want to search for files in the include_path, you can set this parameter to "1".
context

Optional. Specifies the environment of the file handle.

context is a set of options that can modify the behavior of the stream. If null is used, it is ignored.

description

for context support was added in PHP 5.0.0.

Each line in the returned array includes a line ending, so if you do not need line endings, you also need to use the rtrim() function.

Tips and comments

注释:Starting from PHP 4.3.0, you can use file_get_contents() Read the file into a string and return it.

注释:Starting from PHP 4.3.0, the file() function can be used safely for binary files.

注释:If the PHP cannot recognize the line ending of a Macintosh file when reading a file, you can activate the auto_detect_line_endings runtime configuration option.

实例

<?php
print_r(file("test.txt"));
?>

输出:

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?
)