Home > Backend Development > PHP Problem > How to use php file function

How to use php file function

青灯夜游
Release: 2023-02-22 21:48:02
Original
2877 people have browsed it

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)
Copy after login

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"));
?>
Copy after login

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?
)
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template