Writing Files All at Once5:05 with Alena Holligan
Sometimes writing the entire contents of the file at once is the way to go. You can either replace the file or add to the file. Either way opening, writing and closing are all done in a single function.
Be careful when manipulating files!
You can do a lot of damage if you do something wrong. Common errors are: editing the wrong file, filling a hard-drive with garbage data, and deleting the content of a file by accident.
Additional Options for file_put_contents
By default using file_put_contents will REPLACE the contents of the file with the new data. If you want to add (append) the data to an exiting file, you can use the flag FILE_APPEND.
You can specify the data parameter as an array.
Is the same as:
file_put_contents($filename, implode('', $array));
If you are having problems with accessing files, a good place to start looking is at the file permissions. To learn more about file permissions, check out our Console Foundations course, specifically the video on File Permissions
You need to sign up for Treehouse in order to download course files.Sign up