我有一个正在用 fwrite 写入的文本文件,我该如何删除这个文本文件的所有内容,以便我可以重新写入它。我试图找到另一个函数,但没有成功。
我正在使用的示例代码,我想在输入此信息之前清除它:
$string = ', {"key": "'.$info['Name'].'", "value": "'.$info['Name'].'"}';
$fp = fopen('data_old.txt', 'a');
fwrite($fp, $string);
fclose($fp);
最佳答案
如果您查看 fopen
的 PHP 文档,您将在第二个参数中看到可用的“模式”列表。您正在传递 "a"
,这意味着追加。你想传递 "w"
,意思是“写”。
'a' Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
'w' Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
关于php - 如何清除准备好进行 fwrite 的文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10422990/