Home  >  Article  >  Backend Development  >  How to use php file function

How to use php file function

青灯夜游
青灯夜游Original
2019-05-28 15:52:552858browse

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 php file function

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn