fopen()是用来打开文件,在PHP 。
第一个参数的这一功能包含文件的名称将开放第二个参数指定的文件模式应打开:
<?php$file=fopen("welcome.txt","r");
?>
The file may be opened in one of the following modes:
Modes Description
r Read only. Starts at the beginning of the file
r Read/Write. Starts at the beginning of the file
w Write only. Opens and clears the contents of file; or creates a new file if it doesn't exist
w Read/Write. Opens and clears the contents of file; or creates a new file if it doesn't exist
a Append. Opens and writes to the end of the file or creates a new file if it doesn't exist
a Read/Append. Preserves file content by writing to the end of the file
x Write only. Creates a new file. Returns FALSE and an error if file already exists
x Read/Write. Creates a new file. Returns FALSE and an error if file already exists
注意:如果fopen()函数是无法打开指定的文件,它返回0 (假) 。
例如
下面的例子生成一个讯息,如果fopen()函数是无法打开指定的文件:
<?php $file=fopen("welcome.txt","r") or exit("Unable to open file!"); ?> |
fclose 关闭文件
该fclose ( )函数是用来关闭一个开放的档案
<?php $file = fopen("test.txt","r");
//some code to be executed
fclose($file); ?>
检查文件结束
该feof ( )函数检查如果“文件结束” (的EOF )已经达成。
该feof ( )函数是有益的循环,通过数据的未知的长度。
注意:您不能读取档案开放瓦特,一个,和x模式
if (feof($file)) echo "End of file";
读文件一行行
该fgets ( )函数是用来读取一个单一的从一个文件。
注意:在调用此函数的文件指针已经移到下一行。
例如
下面的例子中读取文件一行行,直至到达文件:
<?php $file = fopen("welcome.txt", "r") or exit("Unable to open file!"); //Output a line of the file until the end is reached while(!feof($file)) { echo fgets($file). "
"; } fclose($file); ?>
读文件的性格特征
该fgetc ( )函数是用来读取一个字符从一个档案。
注意:在调用此函数的文件指针移动到下一个字符。
例如
下面的例子中读取一个文件性质的特点,到年底达成的文件是:
<?php $file=fopen("welcome.txt","r") or exit("Unable to open file!"); while (!feof($file)) { echo fgetc($file); } fclose($file); ?> |