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
Let's say that we wanted to list the territories and 0:00 the armed forces, right along with the other states in alphabetical order. 0:02 We'll add a new file named combined_sort. 0:07 We'll start by creating a master array of all the options. 0:18 We'll use the array merge function to merge each of the files we want to use. 0:25 Then we'll use the file function to retrieve each line as a separate 0:35 item in an array. 0:40 I'm going to add an option to the file function, 0:47 that tells the function to ignore the new line characters. 0:50 This will trim the new line characters from each of our elements, so 1:01 that we can keep things clean, and only add line breaks where we specify. 1:04 Territories, And armed forces. 1:20 Next, I want to sort these items. 1:29 But I want to sort them by the display name, 1:31 without looking at any of the HTML tags. 1:33 I'm going to create a function to compare the values without tags. 1:36 CompareStrings. 1:43 We pass the strings we're going to compare, 1:48 And then we use the string case compare. 1:55 This is a binary safe case-insensitive string comparison. 2:01 Then we can use the strip_tags function, On both our values. 2:07 This will just leave the display text to compare. 2:21 We can then return these results. 2:25 Now I can use the user sort function, usort, we pass the array states, 2:28 and then we pass the function that we want to use, compareStrings. 2:37 This will sort our states array, using our compareStrings function. 2:44 Now that our array is ready to go, 2:49 we can write it directly to a file using the file_put_contents. 2:51 File_put_contents uses the same write mode that we used in the last file with fopen, 2:59 but we can also specify a file append flag if we prefer to add the contents 3:05 to the end of an existing file. 3:10 We use implode to join our array as a string, 3:22 and use PHP_EOL as the separator. 3:28 This will give us a nice line break between each option. 3:37 Let's add a select, and include this new file so 3:41 that we can preview it in the browser. 3:43 Let's use sorted instead of combined. 4:02 Then it won't overwrite the other file. 4:08 Now I have a dropdown that includes states, territories, and armed forces, 4:24 all in alphabetical order. 4:28 There are a lot more functions, feature, and options when it comes to reading and 4:32 writing files. 4:36 But until you're actually going to use them, they won't do you much good. 4:37 Scan through the documentation to get an idea of what is available, so 4:42 that when you do need to use it, you're ready. 4:46 Now that you know the basics of reading and 4:49 writing files with PHP, let's add a parsing component in the next section. 4:51 We'll use some of the most common data types, and show how PHP 4:56 can parse those files to create a data driven, personal recommendation site. 5:00
You need to sign up for Treehouse in order to download course files.Sign up