当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > fopen() 函数

PHP
PHP Libxml
PHP Math
PHP Misc
PHP MySQL
PHP SimpleXML
PHP String
PHP XML
PHP Zip
PHP Mail
用PHP5的DirectoryIterators递归扫描目录
PHP 阻止SQL注入式攻击
PHP5面向对象 - 基础 - 类和对象
PHP5面向对象 - 基础 - 类的属性( public )
PHP5面向对象 - 基础 - 类的属性( private )
PHP5面向对象 - 基础 - 方法
PHP5面向对象 - 基础 - 对象的比较
php5面向对象 - 基础 - 构造函数
php5面向对象 - 基础 - 析构函数
用PHP控制用户的浏览器 - ob*函数的使用
PHP PDO 学习笔记

PHP 中的 fopen() 函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-04   浏览: 625 ::
收藏到网摘: n/a

Definition and Usage
定义和用法

The fopen() function opens a file or URL.
fopen()函数的作用是:打开文件或URL。

If fopen() fails, it returns FALSE and an error on failure. You can hide the error output by adding an '@' in front of the function name.
如果fopen()失败,它将返回False并附带错误信息。你可以通过在函数名称职前添加一个“@”来隐藏错误的结果。

Syntax
语法

fopen(filename,mode,include_path,context)

Parameter参数 Description描述
filename Required. Specifies the file or URL to open
必要参数。指定需要打开的文件和URL
mode Required. Specifies the type of access you require to the file/stream.
必要参数。指定需要访问的文件类型

Possible values:
可用值:

  • "r" (Read only. Starts at the beginning of the file)
    “r”(只读格式。始于文件的开头)
  • "r+" (Read/Write. Starts at the beginning of the file)
    “r+”(可读/可写。始于文件的开头)
  • "w" (Write only. Opens and clears the contents of file; or creates a new file if it doesn't exist)
    “w”(只写格式。打开或清除文件的内容;或者创建一个不存在的文件)
  • "w+" (Read/Write. Opens and clears the contents of file; or creates a new file if it doesn't exist)
    “w+”(可读/可写。打开或清除文件的内容;或创建一个不存在的新文件)
  • "a" (Write only. Opens and writes to the end of the file or creates a new file if it doesn't exist)
    “a”(只写格式。打开文件,并在文件的末尾处书写内容;或创建一个新的文件)
  • "a+" (Read/Write. Preserves file content by writing to the end of the file)
    “a+”(可读/可写。通过在文件的末尾书写内容来保存文件的内容)
  • "x" (Write only. Creates a new file. Returns FALSE and an error if file already exists)
    “x”(只写格式。创建一个新的文件)
  • "x+" (Read/Write. Creates a new file. Returns FALSE and an error if file already exists)
    “x+”(可读/可写。创建一个新的文件。如果文件已经存在,则返回False或产生一个错误)
include_path Optional. Set this parameter to '1' if you want to search for the file in the include_path (in php.ini) as well
可选参数。如果你想在include_path(位于php.ini文件中)搜索,将这个参数设置为“1”
context Optional. Specifies the context of the file handle. Context is a set of options that can modify the behavior of a stream
可选参数。指定需要处理的文件内容[context]。Context是一组选项,它是用来修改文本流特征的。


Tips and Notes
注意点:

Note: When writing to a text file, be sure to use the correct line-ending character! Unix systems use n, Windows systems use rn, and Macintosh systems use r as the line ending character. Windows offers a translation flag ('t') which will translate n to rn when working with the file. You can also use 'b' to force binary mode. To use these flags, specify either 'b' or 't' as the last character of the mode parameter.
注意点:当书写一个文本文件时,确认你使用了正确的行结束符[line-ending]。在Unix系统中,行结束符为“n”;在Windows系统中,行结束符为“rn”;在Macintosh系统中,行结束符为“r”。Windows系统中使用了转义符“t”来输出“n”和“rn”;你还可以使用“b”将其转换二进制模式;这个标记指定了“b”或“t”来作为模式参数的最后一个字符。


Example
案例

<?php
$file = fopen("test.txt","r");
$file = fopen("/home/test/test.txt","r");
$file = fopen("/home/test/test.gif","wb");
$file = fopen("http://www.example.com/","r");
$file = fopen("ftp://user:[email protected]/test.txt","w");
?>

评论 (0) All

登陆 | 还没注册?