创建或打开已经存在的文件,重头开始写入新内容:
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "PHP to txt.\n";
fwrite($myfile, $txt);
fclose($myfile);
echo "OK";
打开已经存在的文件,保留原内容,在末尾写入新内容:
$myfile = fopen("newfile.txt", "a+") or die("Unable to open file!");
$txt = "PHP to txt.\n";
fwrite($myfile, $txt);
fclose($myfile);
echo "OK";