好的,以下是使用PHP读写文本文件的演示代码:
读取文本文件:
$filename = "example.txt";
$file = fopen($filename, "r");
if($file) {
while(($line = fgets($file)) !== false) {
echo $line;
}
fclose($file);
}
写入文本文件:
$filename = "example.txt";
$file = fopen($filename, "w");
if($file) {
fwrite($file, "Hello, world!");
fclose($file);
}
以上演示代码中,example.txt
是要读写的文本文件名,使用fopen
函数打开文件,"r"
参数表示以只读模式打开文件,"w"
参数表示以写入模式打开文件,fgets
函数用于逐行读取文件内容,fwrite
函数用于向文件中写入内容。在读取和写入文件结束后,使用fclose
函数关闭文件句柄。