autoclismo wrote: Never tested it but try using 'x' on fread,
The difference between 'x' and 'w' being that with 'x' opening the file for writing will fail if the file already exists – I don't see how that is relevant here.
Damian wrote: I dont know why? it seems random?
I do note that every time you you add a line you write the entire file again. You're writing lines 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 6, ..... You never reset the value $Data after writing its contents.
But don't worry:
fwrite($Handle, $Data, 400000000);
You won't write more than ~400MB for each id.
(Incidentally, [man]fputcsv[/man] can output tab-delimited text.)