Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

file-io

What is better? I have my own MySQL log so it has about 90MB, but I'm not sure which one should I use. It opens file EVERYTIME there is query to execute.

What's faster?

like image 878
genesis Avatar asked Jun 20 '11 18:06

genesis


People also ask

What is the difference between fwrite () and file_put_contents ()?

fwrite() allows to write to file a byte or block of bytes at a time, file_put_content() writes the entire file in one go.... which is better? depends what you need to do! and on the volumes of data that you want to write! fwrite requires a handle while file_put_contents does not.

What does file_ put_ contents return?

The file_put_contents() function returns the number of bytes that were written to the file, or FALSE on failure.


2 Answers

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.

like image 88
hwrdprkns Avatar answered Sep 22 '22 16:09

hwrdprkns


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

like image 26
shaunhusain Avatar answered Sep 18 '22 16:09

shaunhusain