CSV file write question

Hey,

I am allowing my site visitors to fill a small form and put the code in a CSV file. It is working perfectly and i am able to write the data in a CSV file.

I am using fwrite to open the file in Append mode and then using fputscsv for write the data to CSV file and then using fclose to close the file.

I have a little concern about the writing data in a CSV file. For example, if multiple users are filling the form and writing the data at the same time then what will happen ? Will my file be able to write all the data from different users at multiple locations ?

Any thoughts about my concern please ???

Thanks

You need to look at flock() which will lock a file for the thread which called it first. Then unlock it when you’re done so the next script can access it.

Might also consider using a database as opposed to a csv file

I was going to suggest that but then decided against it in case the op is using the csv to export data to another system/program.

Tango - yep; however, its easier to do all inserting, updating, deleting, sorting etc if in db, then export to csv as needed

Oh I’m well aware of that but it wasn’t what the op was actually asking which was why i backtracked on suggesting a db :wink:

I can’t use database as my client needs to have a file so that’s why i have used CSV file.

Thanks

I thought this might be the case.

Look at the flock() function - I supplied a link higher up.