php - Limit file Size while writing

one text

I am in pursuit to create a CSV file, with size constraints defined by Bytes.

For e.g. i need to keep writing to a file, that would contain CSV data, so it doesn't exceed file limit of 5MB.

Without any loss of data in the row.

Obvious solution would be to keep writing the file, and checking its size on every row insert, which could be tiring and lead to lot of disk IO.

Looking for something better to implement with less overhead and that could provide better performance.

Source