当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > PHP 文件处理
The fopen() function is used to open files in PHP.
我们使用PHP的fopen()函数来打开文件。
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> <?php </body> |
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]。
The following example generates a message if the fopen() function is unable to open the specified file:
当fopen()不能打开指定文件,可以产生一条信息,具体代码如下:
<html> <?php </body> |
The fclose() function is used to close an open file:
fclose()函数可用来关闭已打开的文件,如下:
<?php //这个位置可以执行一些打开文件后的代码 fclose($file); |
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 "文件的最后部位"; |
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.
注意:这个函数被请求之后,它会自动跳到下一行。
The example below reads a file line by line, until the end of file is reached:
下面的案例展示了如何逐行读取一个文件,一直读到文件的最末端,具体如下:
<?php |
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.
注意:这个函数被请求之后,它会自动跳到下一个字符。
The example below reads a file character by character, until the end of file is reached:
下面的案例展示了如何逐字读取一个文件,一直读到文件的最后一个字符,具体如下:
<?php |
For a full reference of the PHP filesystem functions, visit our PHP Filesystem Reference.
如果想了解所有的filesystem函数,请参照我们的PHP Filesystem 参数。