
file
(PHP 4, PHP 5)
file — 把整个文件读入一个数组中
说明
array file
( string $filename
[, int $flags = 0
[, resource $context
]] )
把整个文件读入一个数组中。
Note:
你可以通过 file_get_contents() 以字符串形式获取文件的内容。
参数
filename
文件的路径。
Tip
如已启用fopen 包装器,在此函数中, URL 可作为文件名。关于如何指定文件名详见 fopen()。各种
wapper 的不同功能请参见 支持的协议和封装协议,注意其用法及其可提供的预定义变量。
flags
可选参数 flags 可以是以下一个或多个常量:
FILE_USE_INCLUDE_PATH
在 include_path 中查找文件。
FILE_IGNORE_NEW_LINES
在数组每个元素的末尾不要添加换行符
FILE_SKIP_EMPTY_LINES
跳过空行
context
A context resource created with the
stream_context_create() function.
Note: 在 PHP 5.0.0
中增加了对上下文(Context)的支持。有关上下文(Context)的说明参见
Streams。
返回值
Returns the file in an array. Each element of the array corresponds to a
line in the file, with the newline still attached. Upon failure,
file() returns FALSE.
Note:
Each line in the resulting array will include the line ending, unless
FILE_IGNORE_NEW_LINES is used, so you still need to
use rtrim() if you do not want the line ending
present.
Note: 在读取在 Macintosh 电脑中或由其创建的文件时, 如果 PHP
不能正确的识别行结束符,启用运行时配置可选项 auto_detect_line_endings
也许可以解决此问题。
更新日志
版本
说明
5.0.0
增加了参数 context
5.0.0
Prior to PHP 5.0.0 the flags parameter only
covered include_path and was
enabled with 1
4.3.0
file() 开始是二进制安全的
范例
Example #1 file() 例子
//