Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

w+ mode in fopen?

Tags:

php

fopen($handle, 'w+');

I'm looking to open a .CSV file, read each row and do something with a database, and then truncate the .CSV file and write something like

This file has been read.

w+ implies Read/write but the file is also truncated. So what's the point of the read if fopen w+ will just erase what's in it?

like image 445
AlxVallejo Avatar asked Jan 15 '23 20:01

AlxVallejo


1 Answers

Use a+

From the documentation:

'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.
like image 126
Martin Avatar answered Jan 26 '23 02:01

Martin