fwrite vs file_put_contents

Associate
Joined
27 Jun 2006
Posts
1,473
Can anyone tell me the difference (apart from the PHP5 only for php_put_contents).

I wrote a script that should open a file (using fopen) and then take a variable and write it out before closing the file handle (using fwrite and fclose) but do you think it would write it - would it hell!

Kept getting a not a valid stream (I think that was the error) meaning the filehandle wasn't there (even though it created a blank file!)

Scratched my head and then tried php_put_contents and it worked a treat.

Glad it worked but cannot figure out why the fopen/fwrite/fclose commands didn't work.

Any ideas?
 
There isn't really a difference between the two commands. Basically file_put_contents() is the exact same thing as calling fopen(), fwrite() and fclose() in succession to write data to a file.

The reason you probably couldn't get fwrite() to work is because you need to set the mode in which you are opening the file to allow fwrite() to write to the file. The modes in which you can open a file with fopen() are explained in the php manual here.
 
I went through every possible combination of fwrite - check out my hair, torn out in clumps!!!

The script could write to the right place as when I called fopen it wrote the filename ready to get the data but do you think it would write to the file....would it hell!

Anyway, happy that file_put_contents did it magic....saved me hours more pain!
 
Back
Top Bottom