当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > PHP 文件处理

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 文件处理


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

The fopen() function is used to open files in PHP.
我们使用PHP的fopen()函数来打开文件。


Opening a File
用PHP打开一个文件

The fopen() function is used to open files in PHP.
可以使用PHP的fopen()函数来打开文件。

The first parameter of this function contains the name of the file to be opened and the second parameter specifies in which mode the file should be opened:
如下所示:函数中第一个参数包含需要被打开的文件名称,第二个参数指定了打开的方式:

<html>
<body>
<?php
$file=fopen("welcome.txt","r"); //以只读方式打开了与当前php文件同目录下的welcome.txt文件
?>
</body>
</html>

The file may be opened in one of the following modes:
打开的方式具体有如下几种:

Modes
方式
Description
描述
r Read only. Starts at the beginning of the file
只读[Read only]。从文件的开头打开
r+ Read/Write. Starts at the beginning of the file
可读/可写[Read/Write]。从文件的开头打开
w Write only. Opens and clears the contents of file; or creates a new file if it doesn't exist
只写[Write only]。打开文件或清除文件的内容,或者创建一个不存在的文件
w+ Read/Write. Opens and clears the contents of file; or creates a new file if it doesn't exist
可读/可写[Read/Write]。打开文件或清除文件的内容,或者创建一个不存在的文件
a Append. Opens and writes to the end of the file or creates a new file if it doesn't exist
添加[Append]。在文件的末尾打开文件或书写文件的内容,或者创建一个不存在的文件
a+ Read/Append. Preserves file content by writing to the end of the file
可读/添加[Read/Append]。向文件末端书写内容,保存文件内容
x Write only. Creates a new file. Returns FALSE and an error if file already exists
只写[Write only]。创建一个新文件。如果文件已经存在则返回FALSE或一条错误信息
x+ Read/Write. Creates a new file. Returns FALSE and an error if file already exists
可读/可写[Read/Write]。创建一个新文件。如果文件已经存在则返回FALSE或一条错误信息

Note: If the fopen() function is unable to open the specified file, it returns 0 (false).
注意:如果fopen()不能打开指定文件,那么他将返回0[flase]。

Example
案例

The following example generates a message if the fopen() function is unable to open the specified file:
当fopen()不能打开指定文件,可以产生一条信息,具体代码如下:

<html>

<body>
<?php
$file=fopen("welcome.txt","r") or exit("无法打开文件!");
//当找不到welcome.txt这个文件时就会显示无法打开文件!
?>
</body>
</html>

 


Closing a File
关闭文件

The fclose() function is used to close an open file:
fclose()函数可用来关闭已打开的文件,如下:

<?php
$file = fopen("test.txt","r");
//这个位置可以执行一些打开文件后的代码
fclose($file);
?>

 


Check End-of-file
检查是否处于文件末端[End-of-file:EOF]

The feof() function checks if the "end-of-file" (EOF) has been reached.
feof()函数用来检查是否出于文件末端;

The feof() function is useful for looping through data of unknown length.
feof()函数对长度未知的数据执行循环语句非常有用;

Note: You cannot read from files opened in w, a, and x mode!
注意:你不能读取以w,a,x方式打开的文件!

if (feof($file)) echo "文件的最后部位";

 


Reading a File Line by Line
逐行读取文件

The fgets() function is used to read a single line from a file.
Fgets()函数用来读取文件中的一行内容。

Note: After a call to this function the file pointer has moved to the next line.
注意:这个函数被请求之后,它会自动跳到下一行。

Example
案例

The example below reads a file line by line, until the end of file is reached:
下面的案例展示了如何逐行读取一个文件,一直读到文件的最末端,具体如下:

<?php
$file = fopen("welcome.txt", "r") or exit("无法打开文件!");
//逐行输出文件里的信息,直到最后一行结束
while(!feof($file))
{
echo fgets($file). "<br />";
}
fclose($file);
?>

 


Reading a File Character by Character
逐字读取文件

The fgetc() function is used to read a single character from a file.
Fgetc()函数是用来读取一个文件中的一个字符。

Note: After a call to this function the file pointer moves to the next character.
注意:这个函数被请求之后,它会自动跳到下一个字符。

Example
案例

The example below reads a file character by character, until the end of file is reached:
下面的案例展示了如何逐字读取一个文件,一直读到文件的最后一个字符,具体如下:

<?php
$file=fopen("welcome.txt","r") or exit("无法打开文件!");
while (!feof($file))
{
echo fgetc($file);
}
fclose($file);
?>

 


PHP Filesystem Reference
PHP Filesystem 参数

For a full reference of the PHP filesystem functions, visit our PHP Filesystem Reference.
如果想了解所有的filesystem函数,请参照我们的PHP Filesystem 参数

评论 (0) All

登陆 | 还没注册?