maxs — 2006-01-09T17:23:04-05:00 — #1
Which one would be faster and easier on the server? I have a script that rights to a file ever second or so, and when constantly done it empties the file.
Someone suggested switching over to file_put_contents?
Thank you for your input.
f4nat1c — 2006-01-09T17:36:27-05:00 — #2
My preference when coding is file_put_contents.
gavwvin — 2006-01-09T17:41:58-05:00 — #3
According to the manual file_put_contents() is "Identical to calling fopen(), fwrite(), and fclose() successively."... so I suppose if you are writing to the file several times in the same script fwrite() would be better, as it doesn't have to keep opening and closing the file. If you just need to write once then they are identical
maxs — 2006-01-09T17:53:42-05:00 — #4
arborint — 2006-01-09T18:00:36-05:00 — #5
You might want to do some research and see if file_put_contents() caches connections and only closes them at the end. My experience with file_get_contents() is that fopen(), fread(), and fclose() is slightly faster -- though I have no clue why and it really shouldn't be.
f4nat1c — 2006-01-09T18:03:13-05:00 — #6
Sorry. I didn't realise he would be writing to the file consectuively
maxs — 2006-01-09T18:11:04-05:00 — #7
I don't see how caching the connection would help considering hundreds of files are being written to, perhaps 5-10 a second.
arborint — 2006-01-09T18:38:45-05:00 — #8
PHP may not close the file until the script ends or some other condition is met, such as too many files open. So succeeding calls to file_put_contents() may not need to do an fopen(). This is only conjecture on my part though. But I know that PHP tries internally to be smart when dealing with files.
maxs — 2006-01-09T18:47:41-05:00 — #9
It's opening different files, though. Therefore keeping the file open would not be an option.
maxs — 2006-01-09T22:05:20-05:00 — #10
So really, the only way to run successfully a script like this would be to move to a dedicated server?
gavwvin — 2006-01-10T07:20:33-05:00 — #11
if its writing to different files then fopen() fwrite() and fclose() and file_put_contents() are identical: php.net/file_put_contents