f +中的w +模式?

时间:2012-08-13 18:08:25

标签: php

fopen($handle, 'w+');

我正在寻找打开.CSV文件,读取每一行并对数据库执行某些操作,然后截断.CSV文件并编写类似

的内容
  

此文件已被阅读。

w +意味着读/写,但文件也被截断。那么,如果fopen w +只会删除其中的内容,那么读取的重点是什么?

4 个答案:

答案 0 :(得分:6)

使用a+

来自文档:

'w+'     Open for reading and writing; 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.
'a+'     Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it.

答案 1 :(得分:2)

然后在阅读完毕后使用unlink($ filename)。

答案 2 :(得分:0)

在您的情况下,您可能希望以“r”模式打开文件来阅读它。然后关闭它并以“w”模式重新打开以写入截断消息。

答案 3 :(得分:0)

使用' c +'模式。

  

打开文件进行读写。如果该文件不存在,则创建该文件。如果它存在,它既不会被截断(而不是' w'),也不会对此函数的调用失败(例如' x')。文件指针位于文件的开头。

http://php.net/manual/en/function.fopen.php