Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php w+;fopen中的模式?_Php - Fatal编程技术网

Php w+;fopen中的模式?

Php w+;fopen中的模式?,php,Php,我希望打开一个.CSV文件,读取每一行并对数据库执行一些操作,然后截断.CSV文件并编写类似的内容 此文件已被读取 w+表示读/写,但文件也被截断。那么,如果fopen w+只会擦除其中的内容,那么读取又有什么意义呢?在您的情况下,您可能希望以“r”模式打开文件来读取它。然后关闭它并以“w”模式重新打开以写入截断消息。使用a+ 从文件中: fopen($handle, 'w+'); 然后在读完($filename)后使用unlink。使用“c+”模式。 打开文件进行读写。如果文件不存在,则创建

我希望打开一个.CSV文件,读取每一行并对数据库执行一些操作,然后截断.CSV文件并编写类似的内容

此文件已被读取


w+表示读/写,但文件也被截断。那么,如果fopen w+只会擦除其中的内容,那么读取又有什么意义呢?

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

使用
a+

从文件中:

fopen($handle, 'w+');

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

使用“c+”模式。

打开文件进行读写。如果文件不存在,则创建该文件。如果它存在,它既不会被截断(与“w”相反),也不会调用此函数失败(与“x”的情况相同)。文件指针位于文件的开头


w+实际上使您能够将数据写入文件,同时也能够查找到以前写入的位置并再次读取。可能对数据库文件有用吗?w+的要点是,你可以读回你所写的任何东西,而仅仅用“w”是不行的。我已经调整了上面的答案。
'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.