mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-18 13:57:03 +08:00
416 lines
20 KiB
HTML
416 lines
20 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>打开文件或者 URL</title>
|
||
</head>
|
||
<body class="docs"><div id="layout">
|
||
<div id="layout-content"><div id="function.fopen" class="refentry">
|
||
<div class="refnamediv">
|
||
<h1 class="refname">fopen</h1>
|
||
<p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">fopen</span> — <span class="dc-title">打开文件或者 URL</span></p>
|
||
|
||
</div>
|
||
|
||
<div class="refsect1 description" id="refsect1-function.fopen-description">
|
||
<h3 class="title">说明</h3>
|
||
<div class="methodsynopsis dc-description">
|
||
<span class="methodname"><strong>fopen</strong></span>
|
||
( <span class="methodparam"><span class="type">string</span> <code class="parameter">$filename</code></span>
|
||
, <span class="methodparam"><span class="type">string</span> <code class="parameter">$mode</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="type">resource</span></div>
|
||
|
||
<p class="para rdfs-comment">
|
||
<span class="function"><strong>fopen()</strong></span> 将
|
||
<code class="parameter">filename</code>
|
||
指定的名字资源绑定到一个流上。
|
||
</p>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 parameters" id="refsect1-function.fopen-parameters">
|
||
<h3 class="title">参数</h3>
|
||
<p class="para">
|
||
<dl>
|
||
|
||
|
||
<dt>
|
||
<code class="parameter">filename</code></dt>
|
||
|
||
<dd>
|
||
|
||
<p class="para">
|
||
如果
|
||
<code class="parameter">filename</code>
|
||
是 "scheme://..." 的格式,则被当成一个
|
||
URL,PHP 将搜索协议处理器(也被称为封装协议)来处理此模式。如果该协议尚未注册封装协议,PHP
|
||
将发出一条消息来帮助检查脚本中潜在的问题并将
|
||
<code class="parameter">filename</code>
|
||
当成一个普通的文件名继续执行下去。
|
||
</p>
|
||
<p class="para">
|
||
如果 PHP 认为
|
||
<code class="parameter">filename</code>
|
||
指定的是一个本地文件,将尝试在该文件上打开一个流。该文件必须是
|
||
PHP 可以访问的,因此需要确认文件访问权限允许该访问。如果激活了<a href="ini.sect.safe-mode.html#ini.safe-mode" class="link">安全模式</a>或者
|
||
<a href="ini.core.html#ini.open-basedir" class="link">open_basedir</a>
|
||
则会应用进一步的限制。
|
||
</p>
|
||
<p class="para">
|
||
如果 PHP 认为
|
||
<code class="parameter">filename</code>
|
||
指定的是一个已注册的协议,而该协议被注册为一个网络
|
||
URL,PHP 将检查并确认
|
||
<a href="filesystem.configuration.html#ini.allow-url-fopen" class="link">allow_url_fopen</a>
|
||
已被激活。如果关闭了,PHP 将发出一个警告,而 fopen 的调用则失败。
|
||
</p>
|
||
<blockquote class="note"><p><strong class="note">Note</strong>:
|
||
<p class="para">
|
||
所支持的协议列表见<a href="wrappers.html" class="xref">支持的协议和封装协议</a>。某些协议(也被称为
|
||
<em>wrappers</em>)支持
|
||
<em>context</em> 和/或 <var class="filename">php.ini</var>
|
||
选项。参见相应的页面哪些选项可以被设定(例如 <var class="filename">php.ini</var> 中用于
|
||
<em>http</em> wrapper 的 <em>user_agent</em> 值)。
|
||
</p>
|
||
</p></blockquote>
|
||
<p class="para">
|
||
On the Windows platform, be careful to escape any backslashes
|
||
used in the path to the file, or use forward slashes.
|
||
<div class="informalexample">
|
||
<div class="example-contents">
|
||
<div class="phpcode"><pre><span style="color: #000000">
|
||
<span style="color: #0000BB"><?php<br />$handle </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"c:\\folder\\resource.txt"</span><span style="color: #007700">, </span><span style="color: #DD0000">"r"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span>
|
||
</span>
|
||
</pre></div>
|
||
</div>
|
||
|
||
</div>
|
||
</p>
|
||
</dd>
|
||
|
||
|
||
|
||
<dt>
|
||
<code class="parameter">mode</code></dt>
|
||
|
||
<dd>
|
||
|
||
<p class="para">
|
||
<code class="parameter">mode</code>
|
||
参数指定了所要求到该流的访问类型。可以是以下:
|
||
<table class="doctable table">
|
||
<caption><strong>
|
||
<span class="function"><strong>fopen()</strong></span> 中 <code class="parameter">mode</code> 的可能值列表
|
||
</strong></caption>
|
||
|
||
<thead>
|
||
<tr>
|
||
<th><code class="parameter">mode</code></th>
|
||
<th>说明</th>
|
||
</tr>
|
||
|
||
</thead>
|
||
|
||
<tbody class="tbody">
|
||
<tr>
|
||
<td><em>'r'</em></td>
|
||
<td>
|
||
只读方式打开,将文件指针指向文件头。
|
||
</td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<td><em>'r+'</em></td>
|
||
<td>
|
||
读写方式打开,将文件指针指向文件头。
|
||
</td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<td><em>'w'</em></td>
|
||
<td>
|
||
写入方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建之。
|
||
</td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<td><em>'w+'</em></td>
|
||
<td>
|
||
读写方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建之。
|
||
</td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<td><em>'a'</em></td>
|
||
<td>
|
||
写入方式打开,将文件指针指向文件末尾。如果文件不存在则尝试创建之。
|
||
</td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<td><em>'a+'</em></td>
|
||
<td>
|
||
读写方式打开,将文件指针指向文件末尾。如果文件不存在则尝试创建之。
|
||
</td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<td><em>'x'</em></td>
|
||
<td>
|
||
创建并以写入方式打开,将文件指针指向文件头。如果文件已存在,则
|
||
<span class="function"><strong>fopen()</strong></span> 调用失败并返回 <strong><code>FALSE</code></strong>,并生成一条
|
||
<strong><code>E_WARNING</code></strong> 级别的错误信息。如果文件不存在则尝试创建之。这和给
|
||
底层的 <em>open(2)</em> 系统调用指定
|
||
<em>O_EXCL|O_CREAT</em> 标记是等价的。
|
||
</td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<td><em>'x+'</em></td>
|
||
<td>
|
||
创建并以读写方式打开,其他的行为和 <em>'x'</em> 一样。
|
||
</td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<td><em>'c'</em></td>
|
||
<td>
|
||
Open the file for writing only. If the file does not exist, it is
|
||
created. If it exists, it is neither truncated (as opposed to
|
||
<em>'w'</em>), nor the call to this function fails (as is
|
||
the case with <em>'x'</em>). The file pointer is
|
||
positioned on the beginning of the file. This may be useful if it's
|
||
desired to get an advisory lock (see <span class="function"><a href="flock.html" class="function">flock()</a></span>)
|
||
before attempting to modify the file, as using
|
||
<em>'w'</em> could truncate the file before the lock
|
||
was obtained (if truncation is desired,
|
||
<span class="function"><a href="ftruncate.html" class="function">ftruncate()</a></span> can be used after the lock is
|
||
requested).
|
||
</td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<td><em>'c+'</em></td>
|
||
<td>
|
||
Open the file for reading and writing; otherwise it has the same
|
||
behavior as <em>'c'</em>.
|
||
</td>
|
||
</tr>
|
||
|
||
</tbody>
|
||
|
||
</table>
|
||
|
||
</p>
|
||
<blockquote class="note"><p><strong class="note">Note</strong>:
|
||
<p class="para">
|
||
不同的操作系统家族具有不同的行结束习惯。当写入一个文本文件并想插入一个新行时,需要使用符合操作系统的行结束符号。基于
|
||
Unix 的系统使用 <em>\n</em>
|
||
作为行结束字符,基于 Windows 的系统使用 <em>\r\n</em>
|
||
作为行结束字符,基于 Macintosh 的系统使用 <em>\r</em>
|
||
作为行结束字符。
|
||
</p>
|
||
<p class="para">
|
||
如果写入文件时使用了错误的行结束符号,则其它应用程序打开这些文件时可能会表现得很怪异。
|
||
|
||
</p>
|
||
<p class="para">
|
||
Windows 下提供了一个文本转换标记(<em>'t'</em>)可以透明地将
|
||
<em>\n</em> 转换为 <em>\r\n</em>。与此对应还可以使用
|
||
<em>'b'</em> 来强制使用二进制模式,这样就不会转换数据。要使用这些标记,要么用
|
||
<em>'b'</em> 或者用 <em>'t'</em> 作为
|
||
<code class="parameter">mode</code> 参数的最后一个字符。
|
||
</p>
|
||
<p class="para">
|
||
默认的转换模式依赖于 SAPI 和所使用的 PHP
|
||
版本,因此为了便于移植鼓励总是指定恰当的标记。如果是操作纯文本文件并在脚本中使用了
|
||
<em>\n</em> 作为行结束符,但还要期望这些文件可以被其它应用程序例如
|
||
Notepad 读取,则在 mode 中使用
|
||
<em>'t'</em>。在所有其它情况下使用 <em>'b'</em>。
|
||
</p>
|
||
<p class="para">
|
||
在操作二进制文件时如果没有指定 <em>'b'</em>
|
||
标记,可能会碰到一些奇怪的问题,包括坏掉的图片文件以及关于
|
||
<em>\r\n</em> 字符的奇怪问题。
|
||
</p>
|
||
</p></blockquote>
|
||
<blockquote class="note"><p><strong class="note">Note</strong>:
|
||
<p class="para">
|
||
为移植性考虑,强烈建议在用 <span class="function"><strong>fopen()</strong></span>
|
||
打开文件时总是使用 <em>'b'</em> 标记。
|
||
</p>
|
||
</p></blockquote>
|
||
<blockquote class="note"><p><strong class="note">Note</strong>:
|
||
<p class="para">
|
||
再一次,为移植性考虑,强烈建议你重写那些依赖于
|
||
<em>'t'</em> 模式的代码使其使用正确的行结束符并改成
|
||
<em>'b'</em> 模式。
|
||
</p>
|
||
</p></blockquote>
|
||
</dd>
|
||
|
||
|
||
|
||
<dt>
|
||
<code class="parameter">use_include_path</code></dt>
|
||
|
||
<dd>
|
||
|
||
<p class="para">
|
||
如果也需要在
|
||
<a href="ini.core.html#ini.include-path" class="link">include_path</a>
|
||
中搜寻文件的话,可以将可选的第三个参数
|
||
<code class="parameter">use_include_path</code>
|
||
设为 '1' 或 <strong><code>TRUE</code></strong>。
|
||
</p>
|
||
</dd>
|
||
|
||
|
||
|
||
<dt>
|
||
<code class="parameter">context</code></dt>
|
||
|
||
<dd>
|
||
|
||
<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>
|
||
</dd>
|
||
|
||
|
||
</dl>
|
||
|
||
</p>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 returnvalues" id="refsect1-function.fopen-returnvalues">
|
||
<h3 class="title">返回值</h3>
|
||
<p class="para">
|
||
成功时返回文件指针资源,如果打开失败,本函数返回 <strong><code>FALSE</code></strong>。
|
||
</p>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 errors" id="refsect1-function.fopen-errors">
|
||
<h3 class="title">错误/异常</h3>
|
||
<p class="para">
|
||
如果打开失败,会产生一个 <strong><code>E_WARNING</code></strong> 错误。可以通过 <a href="language.operators.errorcontrol.html" class="link">@</a> 来屏蔽错误。
|
||
</p>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 changelog" id="refsect1-function.fopen-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>4.3.2</td>
|
||
<td>
|
||
自 PHP 4.3.2
|
||
起,对所有区别二进制和文本模式的平台默认模式都被设为二进制模式。如果在升级后脚本碰到问题,尝试暂时使用
|
||
<em>'t'</em> 标记,直到所有的脚本都照以下所说的改为更具移植性以后。
|
||
</td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<td>4.3.2</td>
|
||
<td>
|
||
增加了选项 <em>'x'</em> 和 <em>'x+'</em>
|
||
</td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<td>5.2.6</td>
|
||
<td>
|
||
增加了选项 <em>'c'</em> 和 <em>'c+'</em>
|
||
</td>
|
||
</tr>
|
||
|
||
</tbody>
|
||
|
||
</table>
|
||
|
||
</p>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 examples" id="refsect1-function.fopen-examples">
|
||
<h3 class="title">范例</h3>
|
||
<p class="para">
|
||
<div class="example" id="example-2798">
|
||
<p><strong>Example #1 <span class="function"><strong>fopen()</strong></span> 例子</strong></p>
|
||
<div class="example-contents">
|
||
<div class="phpcode"><pre><span style="color: #000000">
|
||
<span style="color: #0000BB"><?php<br />$handle </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"/home/rasmus/file.txt"</span><span style="color: #007700">, </span><span style="color: #DD0000">"r"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$handle </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"/home/rasmus/file.gif"</span><span style="color: #007700">, </span><span style="color: #DD0000">"wb"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$handle </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"http://www.example.com/"</span><span style="color: #007700">, </span><span style="color: #DD0000">"r"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$handle </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"ftp://user:password@example.com/somefile.txt"</span><span style="color: #007700">, </span><span style="color: #DD0000">"w"</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.fopen-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>
|
||
<blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">当启用
|
||
<a href="features.safe_mode.html" class="link">安全模式</a>时,
|
||
PHP 会在执行脚本时检查被脚本操作的目录是否与被执行的脚本有相同的 UID(所有者)。</span></p></blockquote>
|
||
<blockquote class="note"><p><strong class="note">Note</strong>:
|
||
<p class="para">
|
||
如果在用服务器模块版本的 PHP
|
||
时在打开和写入文件上遇到问题,记住要确保所使用的文件和目录是服务器进程所能够访问的。
|
||
</p>
|
||
</p></blockquote>
|
||
<blockquote class="note"><p><strong class="note">Note</strong>:
|
||
<p class="para">
|
||
This function may also succeed when <code class="parameter">filename</code> is a
|
||
directory. If you are unsure whether <code class="parameter">filename</code> is a
|
||
file or a directory, you may need to use the <span class="function"><a href="is_dir.html" class="function">is_dir()</a></span>
|
||
function before calling <span class="function"><strong>fopen()</strong></span>.
|
||
</p>
|
||
</p></blockquote>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 seealso" id="refsect1-function.fopen-seealso">
|
||
<h3 class="title">参见</h3>
|
||
<p class="para">
|
||
<ul class="simplelist">
|
||
<li class="member"><a href="wrappers.html" class="xref">支持的协议和封装协议</a></li>
|
||
<li class="member"><span class="function"><a href="fclose.html" class="function" rel="rdfs-seeAlso">fclose()</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="fwrite.html" class="function" rel="rdfs-seeAlso">fwrite()</a> - 写入文件(可安全用于二进制文件)</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="file.html" class="function" rel="rdfs-seeAlso">file()</a> - 把整个文件读入一个数组中</span></li>
|
||
<li class="member"><span class="function"><a href="file_exists.html" class="function" rel="rdfs-seeAlso">file_exists()</a> - 检查文件或目录是否存在</span></li>
|
||
<li class="member"><span class="function"><a href="is_readable.html" class="function" rel="rdfs-seeAlso">is_readable()</a> - 判断给定文件名是否可读</span></li>
|
||
<li class="member"><span class="function"><a href="stream_set_timeout.html" class="function" rel="rdfs-seeAlso">stream_set_timeout()</a> - Set timeout period on a stream</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="stream_context_create.html" class="function" rel="rdfs-seeAlso">stream_context_create()</a> - 创建资源流上下文</span></li>
|
||
<li class="member"><span class="function"><a href="umask.html" class="function" rel="rdfs-seeAlso">umask()</a> - 改变当前的 umask</span></li>
|
||
<li class="member"><a href="class.splfileobject.html" class="classname">SplFileObject</a></li>
|
||
</ul>
|
||
</p>
|
||
</div>
|
||
|
||
</div></div></div></body></html> |