<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >file</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="PHP 手册" HREF="index.html"><LINK REL="UP" TITLE="Filesystem 文件系统函数" HREF="ref.filesystem.html"><LINK REL="PREVIOUS" TITLE="file_put_contents" HREF="function.file-put-contents.html"><LINK REL="NEXT" TITLE="fileatime" HREF="function.fileatime.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP 手册</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.file-put-contents.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.fileatime.html" ACCESSKEY="N" >下一页</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.file" ></A >file</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN52843" ></A ><P > (PHP 3, PHP 4, PHP 5)</P >file -- 把整个文件读入一个数组中</DIV ><DIV CLASS="refsect1" ><A NAME="AEN52846" ></A ><H2 >说明</H2 >array <B CLASS="methodname" >file</B > ( string filename [, int use_include_path [, resource context]] )<BR ></BR ><P > 和 <A HREF="function.file-get-contents.html" ><B CLASS="function" >file_get_contents()</B ></A > 一样,只除了 <B CLASS="function" >file()</B > 将文件作为一个数组返回。数组中的每个单元都是文件中相应的一行,包括换行符在内。如果失败 <B CLASS="function" >file()</B > 返回 <TT CLASS="constant" ><B >FALSE</B ></TT >。 </P ><P > 如果也想在 <A HREF="ini.core.html#ini.include-path" >include_path</A > 中搜寻文件的话,可以将可选参数 <CODE CLASS="parameter" >use_include_path</CODE > 设为 "1"。 </P ><P > <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN52869" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#FF8000">// 将一个文件读入数组。本例中通过 HTTP 从 URL 中取得 HTML 源文件。<br /></font><font color="#0000BB">$lines </font><font color="#007700">= </font><font color="#0000BB">file</font><font color="#007700">(</font><font color="#DD0000">'http://www.example.com/'</font><font color="#007700">);<br /></font><font color="#FF8000">// 在数组中循环,显示 HTML 的源文件并加上行号。<br /></font><font color="#007700">foreach (</font><font color="#0000BB">$lines </font><font color="#007700">as </font><font color="#0000BB">$line_num </font><font color="#007700">=> </font><font color="#0000BB">$line</font><font color="#007700">) {<br /> echo </font><font color="#DD0000">"Line #<b></font><font color="#007700">{</font><font color="#DD0000">$line_num</font><font color="#007700">}</font><font color="#DD0000"></b> : " </font><font color="#007700">. </font><font color="#0000BB">htmlspecialchars</font><font color="#007700">(</font><font color="#0000BB">$line</font><font color="#007700">) . </font><font color="#DD0000">"<br />\n"</font><font color="#007700">;<br />}<br /></font><font color="#FF8000">// 另一个例子将 web 页面读入字符串。参见 file_get_contents()。<br /></font><font color="#0000BB">$html </font><font color="#007700">= </font><font color="#0000BB">implode</font><font color="#007700">(</font><font color="#DD0000">''</font><font color="#007700">, </font><font color="#0000BB">file </font><font color="#007700">(</font><font color="#DD0000">'http://www.example.com/'</font><font color="#007700">));<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P ></P ></DIV > <DIV CLASS="tip" ><BLOCKQUOTE CLASS="tip" ><P ><B >提示: </B >如果<A HREF="ref.filesystem.html#ini.allow-url-fopen" >“fopen wrappers”</A >已经被激活,则在本函数中可以把 URL 作为文件名来使用。请参阅 <A HREF="function.fopen.html" ><B CLASS="function" >fopen()</B ></A > 函数来获取怎样指定文件名的详细信息以及支持 URL 封装协议的列表:<A HREF="wrappers.html" >附录 N</A >。</P ></BLOCKQUOTE ></DIV > <DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > 返回的数组中每一行都包括了行结束符,因此如果不需要行结束符时还需要使用 <A HREF="function.rtrim.html" ><B CLASS="function" >rtrim()</B ></A > 函数。 </P ></BLOCKQUOTE ></DIV > <DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B >如果碰到 <TT CLASS="literal" >PHP</TT > 在读取文件时不能识别 Macintosh 文件的行结束符,可以激活 <A HREF="ref.filesystem.html#ini.auto-detect-line-endings" >auto_detect_line_endings</A > 运行时配置选项。</P ></BLOCKQUOTE ></DIV > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > 从 PHP 4.3.0 开始可以用 <A HREF="function.file-get-contents.html" ><B CLASS="function" >file_get_contents()</B ></A > 来将文件读入到一个字符串返回。 </P ><P > 从 PHP 4.3.0 开始 <B CLASS="function" >file()</B > 可以安全用于二进制文件。 </P ></BLOCKQUOTE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B >对 context 的支持是 PHP 5.0.0 添加的。有关 <TT CLASS="literal" >context</TT > 的说明见<A HREF="ref.stream.html" >参考 CLX, <I >Stream Functions</I ></A >。</P ></BLOCKQUOTE ></DIV ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >警告</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >当使用 SSL 时,Microsoft IIS 将违反协议不发送 close_notify 标记就关闭连接。PHP 将在到达数据尾端时报告 "SSL: Fatal Protocol Error"。要绕过此问题,应将 <A HREF="ref.errorfunc.html#ini.error-reporting" >error_reporting</A > 级别降低为不包括警告。PHP 4.3.7 及更高版本可以在当使用 https:// 封装协议打开流的时候检测出有此问题的 IIS 服务器并抑制警告。如果使用 <A HREF="function.fsockopen.html" ><B CLASS="function" >fsockopen()</B ></A > 来创建一个 ssl:// 套接字,则需要自己检测并抑制警告信息。</P ></TD ></TR ></TABLE ></DIV ><P > 参见 <A HREF="function.readfile.html" ><B CLASS="function" >readfile()</B ></A >,<A HREF="function.fopen.html" ><B CLASS="function" >fopen()</B ></A >,<A HREF="function.fsockopen.html" ><B CLASS="function" >fsockopen()</B ></A >,<A HREF="function.popen.html" ><B CLASS="function" >popen()</B ></A >,<A HREF="function.file-get-contents.html" ><B CLASS="function" >file_get_contents()</B ></A > 和 <A HREF="function.include.html" ><B CLASS="function" >include()</B ></A >。 </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.file-put-contents.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >起始页</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.fileatime.html" ACCESSKEY="N" >下一页</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >file_put_contents</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.filesystem.html" ACCESSKEY="U" >上一级</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >fileatime</TD ></TR ></TABLE ></DIV ></BODY ></HTML >