What's faster? file_put_contents(); fopen(); fwrite(); fclose();? [closed] What's faster? file_put_contents(); fopen(); fwrite(); fclose();? [closed] php php

What's faster? file_put_contents(); fopen(); fwrite(); fclose();? [closed]


According to this article, the fwrite() is a smidgen faster. (Link to Wayback Machine, because site no longer exists.)

My guess is that file_put_contents() is just a wrapper around those three methods anyways, so you would lose the overhead.

EDIT : This site has the same information.


This function is identical to calling fopen(), fwrite() and fclose() successively to write data to a file.

Check the docs:http://php.net/manual/en/function.file-put-contents.php

Shaun


Depends on your use-case. I've opened files which were over .5 GB, and I certainly didn't want to use file() or file_get_contents(); And file_put_contents couldn't work because I needed to read the file too.

If you're really just interested in appending a file without reading, it doesn't terribly matter; if you're trying to read a whole file into memory (or write a whole file from memory), it similarly does not really matter -- the speed gain, as near as I've seen, is round-off error.

BUT, if you're expecting that these files will ever grow to gargantuan beasts, or if you only need a small subset of the number of lines of a given file, I cannot suggest use of fopen (or the SplFileObject, which is AWESOME) strongly enough -- it is really easy to read from the middle of a file with these.


Since you're just logging, on the other hand, I, personally, find it clearer and more concise to simply use file_put_contents with the append flag. It lets everyone know what's going on without having to look twice.