*打开关闭文件
使用函数 fopen(filename,mode)
为防止文件无法打开的错误,使用下列语句
$file = fopen("welcome.txt","r") or exit("Unable to open file!");
mode 参数:打开文件的方式,一般有
r 只读,从文件开头开始
w 只写,打开并清空文件,如文件不存在,创建新文件
a 追加,打开并向文件末端进行写操作,如文件不存在,创建新文件
fclose(filename) 用来关闭文件
*读写文件
按行读取文件用 fgets(handle)
按字符读取文件用 fgetc(handle)
一般读取文件使用循环操作,循环终止通过检测end-of-file
while(!feof($file))
{
echo fgets($file).'<br/>';
}
写入文件用 fwrite(handle,string)
$string = "I like apple";
$new_file = fopen("banana.txt","a");
fwrite($new_file,$string);
*其他文件函数
file_exist(filename) :查看文件是否存在,返回布尔值
filesize(filename) :返回文件大小,如果文件不存在,报错
unlink(filename) :删除文件,若成功,则返回 true,失败则返回 false,报错
使用函数 fopen(filename,mode)
为防止文件无法打开的错误,使用下列语句
$file = fopen("welcome.txt","r") or exit("Unable to open file!");
mode 参数:打开文件的方式,一般有
r 只读,从文件开头开始
w 只写,打开并清空文件,如文件不存在,创建新文件
a 追加,打开并向文件末端进行写操作,如文件不存在,创建新文件
fclose(filename) 用来关闭文件
*读写文件
按行读取文件用 fgets(handle)
按字符读取文件用 fgetc(handle)
一般读取文件使用循环操作,循环终止通过检测end-of-file
while(!feof($file))
{
echo fgets($file).'<br/>';
}
写入文件用 fwrite(handle,string)
$string = "I like apple";
$new_file = fopen("banana.txt","a");
fwrite($new_file,$string);
*其他文件函数
file_exist(filename) :查看文件是否存在,返回布尔值
filesize(filename) :返回文件大小,如果文件不存在,报错
unlink(filename) :删除文件,若成功,则返回 true,失败则返回 false,报错