mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-18 05:26:57 +08:00
267 lines
15 KiB
HTML
267 lines
15 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-get-contents" class="refentry">
|
||
<div class="refnamediv">
|
||
<h1 class="refname">file_get_contents</h1>
|
||
<p class="verinfo">(PHP 4 >= 4.3.0, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">file_get_contents</span> — <span class="dc-title">将整个文件读入一个字符串</span></p>
|
||
|
||
</div>
|
||
<div class="refsect1 description" id="refsect1-function.file-get-contents-description">
|
||
<h3 class="title">说明</h3>
|
||
<div class="methodsynopsis dc-description">
|
||
<span class="methodname"><strong>file_get_contents</strong></span>
|
||
( <span class="methodparam"><span class="type">string</span> <code class="parameter">$filename</code></span>
|
||
[, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$use_include_path</code><span class="initializer"> = false</span></span>
|
||
[, <span class="methodparam"><span class="type">resource</span> <code class="parameter">$context</code></span>
|
||
[, <span class="methodparam"><span class="type">int</span> <code class="parameter">$offset</code><span class="initializer"> = -1</span></span>
|
||
[, <span class="methodparam"><span class="type">int</span> <code class="parameter">$maxlen</code></span>
|
||
]]]] ) : <span class="type">string</span></div>
|
||
|
||
<p class="para rdfs-comment">
|
||
和 <span class="function"><a href="file.html" class="function">file()</a></span> 一样,只除了
|
||
<span class="function"><strong>file_get_contents()</strong></span> 把文件读入一个字符串。将在参数
|
||
<code class="parameter">offset</code> 所指定的位置开始读取长度为
|
||
<code class="parameter">maxlen</code> 的内容。如果失败,<span class="function"><strong>file_get_contents()</strong></span>
|
||
将返回 <strong><code>FALSE</code></strong>。
|
||
</p>
|
||
<p class="para">
|
||
<span class="function"><strong>file_get_contents()</strong></span>
|
||
函数是用来将文件的内容读入到一个字符串中的首选方法。如果操作系统支持还会使用内存映射技术来增强性能。
|
||
</p>
|
||
<blockquote class="note"><p><strong class="note">Note</strong>:
|
||
<p class="para">
|
||
如果要打开有特殊字符的 URL (比如说有空格),就需要使用
|
||
<span class="function"><a href="urlencode.html" class="function">urlencode()</a></span> 进行 URL 编码。
|
||
</p>
|
||
</p></blockquote>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 parameters" id="refsect1-function.file-get-contents-parameters">
|
||
<h3 class="title">参数</h3>
|
||
<p class="para">
|
||
<dl>
|
||
|
||
|
||
<dt>
|
||
<code class="parameter">filename</code></dt>
|
||
|
||
<dd>
|
||
|
||
<p class="para">
|
||
要读取的文件的名称。
|
||
</p>
|
||
</dd>
|
||
|
||
|
||
|
||
<dt>
|
||
<code class="parameter">use_include_path</code></dt>
|
||
|
||
<dd>
|
||
|
||
<blockquote class="note"><p><strong class="note">Note</strong>:
|
||
<p class="para">
|
||
As of PHP 5 the <strong><code>FILE_USE_INCLUDE_PATH</code></strong> can be used
|
||
to trigger <a href="ini.core.html#ini.include-path" class="link">include path</a>
|
||
search.
|
||
</p>
|
||
</p></blockquote>
|
||
|
||
</dd>
|
||
|
||
|
||
|
||
<dt>
|
||
<code class="parameter">context</code></dt>
|
||
|
||
<dd>
|
||
|
||
<p class="para">
|
||
A valid context resource created with
|
||
<span class="function"><a href="stream_context_create.html" class="function">stream_context_create()</a></span>.
|
||
如果你不需要自定义 context,可以用 <strong><code>NULL</code></strong> 来忽略。
|
||
</p>
|
||
</dd>
|
||
|
||
|
||
|
||
<dt>
|
||
<code class="parameter">offset</code></dt>
|
||
|
||
<dd>
|
||
|
||
<p class="para">
|
||
The offset where the reading starts on the original stream.
|
||
</p>
|
||
<p class="para">
|
||
Seeking (<code class="parameter">offset</code>) is not supported with remote files.
|
||
Attempting to seek on non-local files may work with small offsets, but this
|
||
is unpredictable because it works on the buffered stream.
|
||
</p>
|
||
</dd>
|
||
|
||
|
||
|
||
<dt>
|
||
<code class="parameter">maxlen</code></dt>
|
||
|
||
<dd>
|
||
|
||
<p class="para">
|
||
Maximum length of data read. The default is to read until end
|
||
of file is reached. Note that this parameter is applied to the
|
||
stream processed by the filters.
|
||
</p>
|
||
</dd>
|
||
|
||
|
||
</dl>
|
||
|
||
</p>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 returnvalues" id="refsect1-function.file-get-contents-returnvalues">
|
||
<h3 class="title">返回值</h3>
|
||
<p class="para">
|
||
The function returns the read data 或者在失败时返回 <strong><code>FALSE</code></strong>.
|
||
</p>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 errors" id="refsect1-function.file-get-contents-errors">
|
||
<h3 class="title">错误/异常</h3>
|
||
<p class="para">
|
||
An <strong><code>E_WARNING</code></strong> level error is generated if either <code class="parameter">maxlength</code>
|
||
is less than zero, or if seeking to the specified <code class="parameter">offset</code> in the stream fails.
|
||
</p>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 examples" id="refsect1-function.file-get-contents-examples">
|
||
<h3 class="title">范例</h3>
|
||
<p class="para">
|
||
<div class="example" id="example-2778">
|
||
<p><strong>Example #1 Get and output the source of the homepage of a website</strong></p>
|
||
<div class="example-contents">
|
||
<div class="phpcode"><pre><span style="color: #000000">
|
||
<span style="color: #0000BB"><?php<br />$homepage </span><span style="color: #007700">= </span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'http://www.example.com/'</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">$homepage</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span>
|
||
</span>
|
||
</pre></div>
|
||
</div>
|
||
|
||
</div>
|
||
<div class="example" id="example-2779">
|
||
<p><strong>Example #2 Searching within the include_path</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">// <= PHP 5<br /></span><span style="color: #0000BB">$file </span><span style="color: #007700">= </span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'./people.txt'</span><span style="color: #007700">, </span><span style="color: #0000BB">true</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">// > PHP 5<br /></span><span style="color: #0000BB">$file </span><span style="color: #007700">= </span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'./people.txt'</span><span style="color: #007700">, </span><span style="color: #0000BB">FILE_USE_INCLUDE_PATH</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span>
|
||
</span>
|
||
</pre></div>
|
||
</div>
|
||
|
||
</div>
|
||
<div class="example" id="example-2780">
|
||
<p><strong>Example #3 Reading a section of a file</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">// Read 14 characters starting from the 21st character<br /></span><span style="color: #0000BB">$section </span><span style="color: #007700">= </span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'./people.txt'</span><span style="color: #007700">, </span><span style="color: #0000BB">NULL</span><span style="color: #007700">, </span><span style="color: #0000BB">NULL</span><span style="color: #007700">, </span><span style="color: #0000BB">20</span><span style="color: #007700">, </span><span style="color: #0000BB">14</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$section</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span>
|
||
</span>
|
||
</pre></div>
|
||
</div>
|
||
|
||
<div class="example-contents"><p>以上例程的输出类似于:</p></div>
|
||
<div class="example-contents screen">
|
||
<div class="cdata"><pre>
|
||
string(14) "lle Bjori Ro"
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="example" id="example-2781">
|
||
<p><strong>Example #4 Using stream contexts</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">// Create a stream<br /></span><span style="color: #0000BB">$opts </span><span style="color: #007700">= array(<br /> </span><span style="color: #DD0000">'http'</span><span style="color: #007700">=>array(<br /> </span><span style="color: #DD0000">'method'</span><span style="color: #007700">=></span><span style="color: #DD0000">"GET"</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'header'</span><span style="color: #007700">=></span><span style="color: #DD0000">"Accept-language: en\r\n" </span><span style="color: #007700">.<br /> </span><span style="color: #DD0000">"Cookie: foo=bar\r\n"<br /> </span><span style="color: #007700">)<br />);<br /><br /></span><span style="color: #0000BB">$context </span><span style="color: #007700">= </span><span style="color: #0000BB">stream_context_create</span><span style="color: #007700">(</span><span style="color: #0000BB">$opts</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Open the file using the HTTP headers set above<br /></span><span style="color: #0000BB">$file </span><span style="color: #007700">= </span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'http://www.example.com/'</span><span style="color: #007700">, </span><span style="color: #0000BB">false</span><span style="color: #007700">, </span><span style="color: #0000BB">$context</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span>
|
||
</span>
|
||
</pre></div>
|
||
</div>
|
||
|
||
</div>
|
||
</p>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 changelog" id="refsect1-function.file-get-contents-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.1.0</td>
|
||
<td>
|
||
Added the <code class="parameter">offset</code> and
|
||
<code class="parameter">maxlen</code> parameters.
|
||
</td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<td>5.0.0</td>
|
||
<td>
|
||
Added context support.
|
||
</td>
|
||
</tr>
|
||
|
||
</tbody>
|
||
|
||
</table>
|
||
|
||
</p>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 notes" id="refsect1-function.file-get-contents-notes">
|
||
<h3 class="title">注释</h3>
|
||
<blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">此函数可安全用于二进制对象。</span></p></blockquote>
|
||
<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>
|
||
<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-get-contents-seealso">
|
||
<h3 class="title">参见</h3>
|
||
<p class="para">
|
||
<ul class="simplelist">
|
||
<li class="member"><span class="function"><a href="file.html" class="function" rel="rdfs-seeAlso">file()</a> - 把整个文件读入一个数组中</span></li>
|
||
<li class="member"><span class="function"><a href="fgets.html" class="function" rel="rdfs-seeAlso">fgets()</a> - 从文件指针中读取一行</span></li>
|
||
<li class="member"><span class="function"><a href="fread.html" class="function" rel="rdfs-seeAlso">fread()</a> - 读取文件(可安全用于二进制文件)</span></li>
|
||
<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="file_put_contents.html" class="function" rel="rdfs-seeAlso">file_put_contents()</a> - 将一个字符串写入文件</span></li>
|
||
<li class="member"><span class="function"><a href="stream_get_contents.html" class="function" rel="rdfs-seeAlso">stream_get_contents()</a> - 读取资源流到一个字符串</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>
|
||
<li class="member"><a href="reserved.variables.httpresponseheader.html" class="link">$http_response_header</a></li>
|
||
</ul>
|
||
</p>
|
||
</div>
|
||
|
||
</div></div></div></body></html> |