PHP 中的 file() 函数

phpprogrammingserver side programming

file() 函数将文件读入数组。

语法

file(file_path,flag,context)

参数

  • file − 文件的路径。

  • flag − 可选参数 flags 可以是以下一个或多个常量 −

    • FILE_USE_INCLUDE_PATH − 在 include_path 中搜索文件。

    • FILE_IGNORE_NEW_LINES −不要在每个数组元素的末尾添加换行符。

    • FILE_SKIP_EMPTY_LINES − 跳过空行。

    • FILE_TEXT − 内容以 UTF-8 编码返回。您可以通过创建自定义上下文来指定不同的编码。此标志不能与 FILE_BINARY 一起使用。此标志仅在 PHP 6 以上可用。

    • FILE_BINARY − 内容以二进制数据读取。这是默认设置,不能与 FILE_TEXT 一起使用。此标志仅在 PHP 6 之后可用。

  • context − 它修改了流的行为。

返回

file() 函数以数组形式返回文件,失败时返回 false。

示例

假设我们有一个文件"continents.txt",其中包含以下内容和行。

The Earth has seven continents.
The continents are: Asia, Africa, North America, South America, Antarctica, Europe, and Australia.
Asia is the largest in area.
Australia is the smallest in terms of area.

示例

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

输出

Array
(
   [0] => The Earth has seven continents.
   [1] => The continents are: Asia, Africa, North America, South America, Antarctica, Europe, and Australia.
   [2] => Asia is the largest in area.
   [3] => Australia is the smallest in terms of area.
)

相关文章