This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
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.
Downloads
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.
file_put_contents($filename, $array);
Is the same as:
file_put_contents($filename, implode('', $array));
Read more in the documentation.
PERMISSIONS
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
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up