0

When using file_put_contents in for loop, I tried this

for(loop) {
    file_put_contents("content ");
}

So if power failure occurs in middle of loop.
Some data should be written in the file.
But it is blank

I have the same problem with fputcsv()
I am closing and opening the file on every iteration of for loop when using fputcsv()

I tried this

for(loop) {
    open file;
    fputcsv();
    close;
}
Touki
  • 7,465
  • 3
  • 41
  • 63
  • 4
    It isn't guaranteed that any data will be written to disk... you're writing to a file, but the server won't write every line to disk as you send it (which would be incredibly slow); rather it will cache until a significant amount has been written to the file before it physically stores it on disk – Mark Baker Sep 05 '13 at 12:51
  • I've not tried it but there are a number of factors that could cause it not to be written to disk. I take it that this is the same file you are opening? If so, I suggest you open it before your loop, close after your loop and use `fflush` after writing data to it. I'm not sure if `fputcsv` adds data to the file incrementally or if it just truncates it but incrementally would be the way to go. @MarkBaker is correct and I don't recommend you do it. – pedro_sland Sep 05 '13 at 12:51
  • 2
    But if the data you're writing is that critical, then you should be using a UPS to protect against power failure – Mark Baker Sep 05 '13 at 12:51
  • Here failure is acceptable in web apps...its not used in a nuclear reactor :) – Arun Killu Sep 05 '13 at 12:52
  • 1
    Please show some actual code, not this pseudo-code. There are some important details that are missing, like whether you're opening the file in truncate or append mode. – Barmar Sep 05 '13 at 13:13
  • u guys are not helping – user2750711 Sep 10 '13 at 09:34

0 Answers0