ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
# PHP file() 函数 ## 定义和用法 file() 函数把整个文件读入一个数组中。 与 [file_get_contents()](/php/func_filesystem_file_get_contents.asp "PHP file_get_contents() 函数") 类似,不同的是 file() 将文件作为一个数组返回。数组中的每个单元都是文件中相应的一行,包括换行符在内。 如果失败,则返回 false。 ### 语法 ``` file(path,include_path,context) ``` | 参数 | 描述 | | --- | --- | | path | 必需。规定要读取的文件。 | | include_path | 可选。如果也想在 include_path 中搜寻文件的话,可以将该参数设为 "1"。 | | context | 可选。规定文件句柄的环境。context 是一套可以修改流的行为的选项。若使用 null,则忽略。 | ### 说明 对 _context_ 的支持是 PHP 5.0.0 添加的。 返回的数组中每一行都包括了行结束符,因此如果不需要行结束符时还需要使用 rtrim() 函数。 ## 提示和注释 注释:从 PHP 4.3.0 开始,可以用 [file_get_contents()](/php/func_filesystem_file_get_contents.asp "PHP file_get_contents() 函数") 来将文件读入到一个字符串并返回。 注释:从 PHP 4.3.0 开始,file() 可以安全用于二进制文件。 注释:如果碰到 PHP 在读取文件时不能识别 Macintosh 文件的行结束符,可以激活 auto_detect_line_endings 运行时配置选项。 ## 例子 ``` <?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? ) ```