(PHP 4, PHP 5)
fopen — 打开文件或者 URL
$filename
, string $mode
[, bool $use_include_path
[, resource $zcontext
]] )
fopen() 将
filename
指定的名字资源绑定到一个流上。如果
filename
是 "scheme://..." 的格式,则被当成一个
URL,PHP 将搜索协议处理器(也被称为封装协议)来处理此模式。如果该协议尚未注册封装协议,PHP
将发出一条消息来帮助检查脚本中潜在的问题并将
filename
当成一个普通的文件名继续执行下去。
如果 PHP 认为
filename
指定的是一个本地文件,将尝试在该文件上打开一个流。该文件必须是
PHP 可以访问的,因此需要确认文件访问权限允许该访问。如果激活了安全模式或者
open_basedir
则会应用进一步的限制。
如果 PHP 认为
filename
指定的是一个已注册的协议,而该协议被注册为一个网络
URL,PHP 将检查并确认
allow_url_fopen
已被激活。如果关闭了,PHP 将发出一个警告,而 fopen 的调用则失败。
Note: 所支持的协议列表见Supported Protocols and Wrappers。某些协议(也被称为 wrappers)支持 context 和/或 php.ini 选项。参见相应的页面哪些选项可以被设定(例如 php.ini 中用于 http wrapper 的 user_agent 值)。
Note: 在 PHP 5.0.0 中增加了 对上下文(Context)的支持。 有关 上下文(Context) 的说明参见 Streams。
Note: 自 PHP 4.3.2 起,对所有区别二进制和文本模式的平台默认模式都被设为二进制模式。如果在升级后脚本碰到问题,尝试暂时使用 't' 标记,直到所有的脚本都照以下所说的改为更具移植性以后。
mode
参数指定了所要求到该流的访问类型。可以是以下:
mode |
说明 |
---|---|
'r' | 只读方式打开,将文件指针指向文件头。 |
'r+' | 读写方式打开,将文件指针指向文件头。 |
'w' | 写入方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建之。 |
'w+' | 读写方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建之。 |
'a' | 写入方式打开,将文件指针指向文件末尾。如果文件不存在则尝试创建之。 |
'a+' | 读写方式打开,将文件指针指向文件末尾。如果文件不存在则尝试创建之。 |
'x' |
创建并以写入方式打开,将文件指针指向文件头。如果文件已存在,则
fopen() 调用失败并返回 FALSE ,并生成一条
E_WARNING 级别的错误信息。如果文件不存在则尝试创建之。这和给
底层的 open(2) 系统调用指定
O_EXCL|O_CREAT 标记是等价的。此选项被
PHP 4.3.2 以及以后的版本所支持,仅能用于本地文件。
|
'x+' |
创建并以读写方式打开,将文件指针指向文件头。如果文件已存在,则
fopen() 调用失败并返回 FALSE ,并生成一条
E_WARNING 级别的错误信息。如果文件不存在则尝试创建之。这和给
底层的 open(2) 系统调用指定
O_EXCL|O_CREAT 标记是等价的。此选项被
PHP 4.3.2 以及以后的版本所支持,仅能用于本地文件。
|
Note:
不同的操作系统家族具有不同的行结束习惯。当写入一个文本文件并想插入一个新行时,需要使用符合操作系统的行结束符号。基于 Unix 的系统使用 \n 作为行结束字符,基于 Windows 的系统使用 \r\n 作为行结束字符,基于 Macintosh 的系统使用 \r 作为行结束字符。
如果写入文件时使用了错误的行结束符号,则其它应用程序打开这些文件时可能会表现得很怪异。
Windows 下提供了一个文本转换标记('t')可以透明地将 \n 转换为 \r\n。与此对应还可以使用 'b' 来强制使用二进制模式,这样就不会转换数据。要使用这些标记,要么用 'b' 或者用 't' 作为
mode
参数的最后一个字符。默认的转换模式依赖于 SAPI 和所使用的 PHP 版本,因此为了便于移植鼓励总是指定恰当的标记。如果是操作纯文本文件并在脚本中使用了 \n 作为行结束符,但还要期望这些文件可以被其它应用程序例如 Notepad 读取,则在 mode 中使用 't'。在所有其它情况下使用 'b'。
在操作二进制文件时如果没有指定 'b' 标记,可能会碰到一些奇怪的问题,包括坏掉的图片文件以及关于 \r\n 字符的奇怪问题。
Note:
为移植性考虑,强烈建议在用 fopen() 打开文件时总是使用 'b' 标记。
Note:
再一次,为移植性考虑,强烈建议你重写那些依赖于 't' 模式的代码使其使用正确的行结束符并改成 'b' 模式。
如果也需要在
include_path
中搜寻文件的话,可以将可选的第三个参数
use_include_path
设为 '1' 或 TRUE
。
如果打开失败,本函数返回 FALSE
。
Example #1 fopen() 例子
<?php
$handle = fopen("/home/rasmus/file.txt", "r");
$handle = fopen("/home/rasmus/file.gif", "wb");
$handle = fopen("http://www.example.com/", "r");
$handle = fopen("ftp://user:password@example.com/somefile.txt", "w");
?>
如果在用服务器模块版本的 PHP 时在打开和写入文件上遇到问题,记住要确保所使用的文件是服务器进程所能够访问的。
在 Windows 平台上,要小心转义文件路径中的每个反斜线,或者用斜线。
<?php
$handle = fopen("c:\\data\\info.txt", "r");
?>
使用 SSL 时,Microsoft IIS 会违反协议不发送close_notify标记就关闭连接。PHP 会在到达数据尾端时报告“SSL: Fatal Protocol Error”。 要解决此问题,error_reporting 应设定为降低级别至不包含警告。 PHP 4.3.7 及更高版本可以在使用 https:// 包装器打开流时检测出有问题的 IIS 服务器软件 并抑制警告。在使用 fsockopen() 创建 ssl:// 套接字时, 开发者需检测并抑制此警告。
Note: 当启用 安全模式时, PHP 会在执行脚本时检查被脚本操作的目录是否与被执行的脚本有相同的 UID(所有者)。
参见Supported Protocols and Wrappers,fclose(),fgets(),fread(),fwrite(),fsockopen(),file(),file_exists(),is_readable(),stream_set_timeout() 和 popen()。
filename
If filename
is of the form "scheme://...", it
is assumed to be a URL and PHP will search for a protocol handler
(also known as a wrapper) for that scheme. If no wrappers for that
protocol are registered, PHP will emit a notice to help you track
potential problems in your script and then continue as though
filename
specifies a regular file.
If PHP has decided that filename
specifies
a local file, then it will try to open a stream on that file.
The file must be accessible to PHP, so you need to ensure that
the file access permissions allow this access.
If you have enabled 安全模式,
or open_basedir further
restrictions may apply.
If PHP has decided that filename
specifies
a registered protocol, and that protocol is registered as a
network URL, PHP will check to make sure that
allow_url_fopen is
enabled. If it is switched off, PHP will emit a warning and
the fopen call will fail.
Note:
The list of supported protocols can be found in Supported Protocols and Wrappers. Some protocols (also referred to as wrappers) support context and/or php.ini options. Refer to the specific page for the protocol in use for a list of options which can be set. (e.g. php.ini value user_agent used by the http wrapper).
On the Windows platform, be careful to escape any backslashes used in the path to the file, or use forward slashes.
<?php
$handle = fopen("c:\folder\resource.txt", "r");
?>
mode
The mode
parameter specifies the type of access
you require to the stream. It may be any of the following:
mode |
Description |
---|---|
'r' | Open for reading only; place the file pointer at the beginning of the file. |
'r+' | Open for reading and writing; place the file pointer at the beginning of the file. |
'w' | Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it. |
'w+' | Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it. |
'a' | Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it. |
'a+' | Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it. |
'x' |
Create and open for writing only; place the file pointer at the
beginning of the file. If the file already exists, the
fopen() call will fail by returning FALSE and
generating an error of level E_WARNING . If
the file does not exist, attempt to create it. This is equivalent
to specifying O_EXCL|O_CREAT flags for the
underlying open(2) system call.
|
'x+' | Create and open for reading and writing; otherwise it has the same behavior as 'x'. |
'c' | 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 'w'), nor the call to this function fails (as is the case with 'x'). 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 flock()) before attempting to modify the file, as using 'w' could truncate the file before the lock was obtained (if truncation is desired, ftruncate() can be used after the lock is requested). |
'c+' | Open the file for reading and writing; otherwise it has the same behavior as 'c'. |
Note:
Different operating system families have different line-ending conventions. When you write a text file and want to insert a line break, you need to use the correct line-ending character(s) for your operating system. Unix based systems use \n as the line ending character, Windows based systems use \r\n as the line ending characters and Macintosh based systems use \r as the line ending character.
If you use the wrong line ending characters when writing your files, you might find that other applications that open those files will "look funny".
Windows offers a text-mode translation flag ('t') which will transparently translate \n to \r\n when working with the file. In contrast, you can also use 'b' to force binary mode, which will not translate your data. To use these flags, specify either 'b' or 't' as the last character of the
mode
parameter.The default translation mode depends on the SAPI and version of PHP that you are using, so you are encouraged to always specify the appropriate flag for portability reasons. You should use the 't' mode if you are working with plain-text files and you use \n to delimit your line endings in your script, but expect your files to be readable with applications such as notepad. You should use the 'b' in all other cases.
If you do not specify the 'b' flag when working with binary files, you may experience strange problems with your data, including broken image files and strange problems with \r\n characters.
Note:
For portability, it is strongly recommended that you always use the 'b' flag when opening files with fopen().
Note:
Again, for portability, it is also strongly recommended that you re-write code that uses or relies upon the 't' mode so that it uses the correct line endings and 'b' mode instead.
use_include_path
The optional third use_include_path
parameter
can be set to '1' or TRUE
if you want to search for the file in the
include_path, too.
context
Note: 在 PHP 5.0.0 中增加了 对上下文(Context)的支持。 有关 上下文(Context) 的说明参见 Streams。
Returns a file pointer resource on success, or FALSE
on error.
If the open fails, an error of level
E_WARNING
is generated. You may use @ to suppress this
warning.
版本 | 说明 |
---|---|
4.3.2 | As of PHP 4.3.2, the default mode is set to binary for all platforms that distinguish between binary and text mode. If you are having problems with your scripts after upgrading, try using the 't' flag as a workaround until you have made your script more portable as mentioned before |
4.3.2 | The 'x' and 'x+' options were added |
5.2.6 | The 'c' and 'c+' options were added |
Example #2 fopen() examples
<?php
$handle = fopen("/home/rasmus/file.txt", "r");
$handle = fopen("/home/rasmus/file.gif", "wb");
$handle = fopen("http://www.example.com/", "r");
$handle = fopen("ftp://user:password@example.com/somefile.txt", "w");
?>
使用 SSL 时,Microsoft IIS 会违反协议不发送close_notify标记就关闭连接。PHP 会在到达数据尾端时报告“SSL: Fatal Protocol Error”。 要解决此问题,error_reporting 应设定为降低级别至不包含警告。 PHP 4.3.7 及更高版本可以在使用 https:// 包装器打开流时检测出有问题的 IIS 服务器软件 并抑制警告。在使用 fsockopen() 创建 ssl:// 套接字时, 开发者需检测并抑制此警告。
Note: 当启用 安全模式时, PHP 会在执行脚本时检查被脚本操作的目录是否与被执行的脚本有相同的 UID(所有者)。
If you are experiencing problems with reading and writing to files and you're using the server module version of PHP, remember to make sure that the files and directories you're using are accessible to the server process.