mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-17 21:16:57 +08:00
246 lines
11 KiB
HTML
246 lines
11 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||
<html>
|
||
<head>
|
||
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
||
<title>把整个文件读入一个数组中</title>
|
||
</head>
|
||
<body class="docs"><div id="layout">
|
||
<div id="layout-content"><div id="function.file" class="refentry">
|
||
<div class="refnamediv">
|
||
<h1 class="refname">file</h1>
|
||
<p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">file</span> — <span class="dc-title">把整个文件读入一个数组中</span></p>
|
||
|
||
</div>
|
||
|
||
<div class="refsect1 description" id="refsect1-function.file-description">
|
||
<h3 class="title">说明</h3>
|
||
<div class="methodsynopsis dc-description">
|
||
<span class="methodname"><strong>file</strong></span>
|
||
( <span class="methodparam"><span class="type">string</span> <code class="parameter">$filename</code></span>
|
||
[, <span class="methodparam"><span class="type">int</span> <code class="parameter">$flags</code><span class="initializer"> = 0</span></span>
|
||
[, <span class="methodparam"><span class="type">resource</span> <code class="parameter">$context</code></span>
|
||
]] ) : <span class="type">array</span></div>
|
||
|
||
<p class="para rdfs-comment">
|
||
把整个文件读入一个数组中。
|
||
</p>
|
||
<blockquote class="note"><p><strong class="note">Note</strong>:
|
||
<p class="para">
|
||
你可以通过 <span class="function"><a href="file_get_contents.html" class="function">file_get_contents()</a></span> 以字符串形式获取文件的内容。
|
||
</p>
|
||
</p></blockquote>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 parameters" id="refsect1-function.file-parameters">
|
||
<h3 class="title">参数</h3>
|
||
<p class="para">
|
||
<dl>
|
||
|
||
|
||
<dt>
|
||
<code class="parameter">filename</code></dt>
|
||
|
||
<dd>
|
||
|
||
<p class="para">
|
||
文件的路径。
|
||
</p>
|
||
<div class="tip"><strong class="tip">Tip</strong><p class="simpara">如已启用<a href="filesystem.configuration.html#ini.allow_url_fopen" class="link">fopen 包装器</a>,在此函数中, URL 可作为文件名。关于如何指定文件名详见 <span class="function"><a href="fopen.html" class="function">fopen()</a></span>。各种
|
||
wapper 的不同功能请参见 <a href="wrappers.html" class="xref">支持的协议和封装协议</a>,注意其用法及其可提供的预定义变量。</p></div>
|
||
</dd>
|
||
|
||
|
||
|
||
<dt>
|
||
<code class="parameter">flags</code></dt>
|
||
|
||
<dd>
|
||
|
||
<p class="para">
|
||
可选参数 <code class="parameter">flags</code> 可以是以下一个或多个常量:
|
||
<dl>
|
||
|
||
|
||
<dt>
|
||
|
||
<strong><code>FILE_USE_INCLUDE_PATH</code></strong>
|
||
</dt>
|
||
|
||
<dd>
|
||
|
||
<span class="simpara">
|
||
在 <a href="ini.core.html#ini.include-path" class="link">include_path</a> 中查找文件。
|
||
</span>
|
||
</dd>
|
||
|
||
|
||
|
||
<dt>
|
||
|
||
<strong><code>FILE_IGNORE_NEW_LINES</code></strong>
|
||
</dt>
|
||
|
||
<dd>
|
||
|
||
<span class="simpara">
|
||
在数组每个元素的末尾不要添加换行符
|
||
</span>
|
||
</dd>
|
||
|
||
|
||
|
||
<dt>
|
||
|
||
<strong><code>FILE_SKIP_EMPTY_LINES</code></strong>
|
||
</dt>
|
||
|
||
<dd>
|
||
|
||
<span class="simpara">
|
||
跳过空行
|
||
</span>
|
||
</dd>
|
||
|
||
|
||
|
||
</dl>
|
||
|
||
</p>
|
||
</dd>
|
||
|
||
|
||
|
||
<dt>
|
||
<code class="parameter">context</code></dt>
|
||
|
||
<dd>
|
||
|
||
<p class="para">
|
||
A context resource created with the
|
||
<span class="function"><a href="stream_context_create.html" class="function">stream_context_create()</a></span> function.
|
||
</p>
|
||
<p class="para">
|
||
<blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">在 PHP 5.0.0
|
||
中增加了对上下文(Context)的支持。有关<em>上下文(Context)</em>的说明参见
|
||
<a href="book.stream.html" class="xref">Streams</a>。</span></p></blockquote>
|
||
</p>
|
||
</dd>
|
||
|
||
|
||
</dl>
|
||
|
||
</p>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 returnvalues" id="refsect1-function.file-returnvalues">
|
||
<h3 class="title">返回值</h3>
|
||
<p class="para">
|
||
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,
|
||
<span class="function"><strong>file()</strong></span> returns <strong><code>FALSE</code></strong>.
|
||
</p>
|
||
<blockquote class="note"><p><strong class="note">Note</strong>:
|
||
<p class="para">
|
||
Each line in the resulting array will include the line ending, unless
|
||
<strong><code>FILE_IGNORE_NEW_LINES</code></strong> is used, so you still need to
|
||
use <span class="function"><a href="rtrim.html" class="function">rtrim()</a></span> if you do not want the line ending
|
||
present.
|
||
</p>
|
||
</p></blockquote>
|
||
<blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">在读取在 Macintosh 电脑中或由其创建的文件时, 如果 PHP
|
||
不能正确的识别行结束符,启用运行时配置可选项 <a href="filesystem.configuration.html#ini.auto-detect-line-endings" class="link">auto_detect_line_endings</a>
|
||
也许可以解决此问题。</span></p></blockquote>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 changelog" id="refsect1-function.file-changelog">
|
||
<h3 class="title">更新日志</h3>
|
||
<p class="para">
|
||
<table class="doctable informaltable">
|
||
|
||
<thead>
|
||
<tr>
|
||
<th>版本</th>
|
||
<th>说明</th>
|
||
</tr>
|
||
|
||
</thead>
|
||
|
||
<tbody class="tbody">
|
||
|
||
<tr>
|
||
<td>5.0.0</td>
|
||
<td>
|
||
增加了参数 <code class="parameter">context</code>
|
||
</td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<td>5.0.0</td>
|
||
<td>
|
||
Prior to PHP 5.0.0 the <code class="parameter">flags</code> parameter only
|
||
covered <a href="ini.core.html#ini.include-path" class="link">include_path</a> and was
|
||
enabled with 1
|
||
</td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<td>4.3.0</td>
|
||
<td>
|
||
<span class="function"><strong>file()</strong></span> 开始是二进制安全的
|
||
</td>
|
||
</tr>
|
||
|
||
</tbody>
|
||
|
||
</table>
|
||
|
||
</p>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 examples" id="refsect1-function.file-examples">
|
||
<h3 class="title">范例</h3>
|
||
<p class="para">
|
||
<div class="example" id="example-2784">
|
||
<p><strong>Example #1 <span class="function"><strong>file()</strong></span> 例子</strong></p>
|
||
<div class="example-contents">
|
||
<div class="phpcode"><pre><span style="color: #000000">
|
||
<span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// 将一个文件读入数组。本例中通过 HTTP 从 URL 中取得 HTML 源文件。<br /><br /></span><span style="color: #0000BB">$lines </span><span style="color: #007700">= </span><span style="color: #0000BB">file</span><span style="color: #007700">(</span><span style="color: #DD0000">'http://www.example.com/'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// 在数组中循环,显示 HTML 的源文件并加上行号。<br /><br /></span><span style="color: #007700">foreach (</span><span style="color: #0000BB">$lines </span><span style="color: #007700">as </span><span style="color: #0000BB">$line_num </span><span style="color: #007700">=> </span><span style="color: #0000BB">$line</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"Line #<b></span><span style="color: #007700">{</span><span style="color: #0000BB">$line_num</span><span style="color: #007700">}</span><span style="color: #DD0000"></b> : " </span><span style="color: #007700">. </span><span style="color: #0000BB">htmlspecialchars</span><span style="color: #007700">(</span><span style="color: #0000BB">$line</span><span style="color: #007700">) . </span><span style="color: #DD0000">"<br />\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">// 另一个例子将 web 页面读入字符串。参见 file_get_contents()。<br /><br /></span><span style="color: #0000BB">$html </span><span style="color: #007700">= </span><span style="color: #0000BB">implode</span><span style="color: #007700">(</span><span style="color: #DD0000">''</span><span style="color: #007700">, </span><span style="color: #0000BB">file</span><span style="color: #007700">(</span><span style="color: #DD0000">'http://www.example.com/'</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">// 从 PHP 5 开始可以使用可选标记参数<br /></span><span style="color: #0000BB">$trimmed </span><span style="color: #007700">= </span><span style="color: #0000BB">file</span><span style="color: #007700">(</span><span style="color: #DD0000">'somefile.txt'</span><span style="color: #007700">, </span><span style="color: #0000BB">FILE_IGNORE_NEW_LINES </span><span style="color: #007700">| </span><span style="color: #0000BB">FILE_SKIP_EMPTY_LINES</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span>
|
||
</span>
|
||
</pre></div>
|
||
</div>
|
||
|
||
</div>
|
||
</p>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 notes" id="refsect1-function.file-notes">
|
||
<h3 class="title">注释</h3>
|
||
<div class="warning"><strong class="warning">Warning</strong><p class="para">使用 SSL 时,Microsoft IIS
|
||
会违反协议不发送<em>close_notify</em>标记就关闭连接。PHP 会在到达数据尾端时报告“SSL: Fatal Protocol Error”。
|
||
要解决此问题,<a href="errorfunc.configuration.html#ini.error-reporting" class="link">error_reporting</a> 应设定为降低级别至不包含警告。
|
||
PHP 4.3.7 及更高版本可以在使用 <em>https://</em> 包装器打开流时检测出有问题的 IIS 服务器软件 并抑制警告。在使用
|
||
<span class="function"><a href="fsockopen.html" class="function">fsockopen()</a></span> 创建 <em>ssl://</em> 套接字时, 开发者需检测并抑制此警告。</p></div>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 seealso" id="refsect1-function.file-seealso">
|
||
<h3 class="title">参见</h3>
|
||
<p class="para">
|
||
<ul class="simplelist">
|
||
<li class="member"><span class="function"><a href="readfile.html" class="function" rel="rdfs-seeAlso">readfile()</a> - 输出文件</span></li>
|
||
<li class="member"><span class="function"><a href="fopen.html" class="function" rel="rdfs-seeAlso">fopen()</a> - 打开文件或者 URL</span></li>
|
||
<li class="member"><span class="function"><a href="fsockopen.html" class="function" rel="rdfs-seeAlso">fsockopen()</a> - 打开一个网络连接或者一个Unix套接字连接</span></li>
|
||
<li class="member"><span class="function"><a href="popen.html" class="function" rel="rdfs-seeAlso">popen()</a> - 打开进程文件指针</span></li>
|
||
<li class="member"><span class="function"><a href="file_get_contents.html" class="function" rel="rdfs-seeAlso">file_get_contents()</a> - 将整个文件读入一个字符串</span></li>
|
||
<li class="member"><span class="function"><a href="include.html" class="function" rel="rdfs-seeAlso">include</a> - include</span></li>
|
||
<li class="member"><span class="function"><a href="stream_context_create.html" class="function" rel="rdfs-seeAlso">stream_context_create()</a> - 创建资源流上下文</span></li>
|
||
</ul>
|
||
</p>
|
||
</div>
|
||
|
||
</div></div></div></body></html> |