Writing Files Line by Line5:25 with Alena Holligan
Writing files builds upon the same approaches as reading files. Writing files will allow us to save any changes we make, and also allow us to change settings and update application details.
Download: File Handling Cheat Sheet
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.
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
File Functions and Modes
|r||Open a file for read only. File pointer starts at the beginning of the file|
|w||Open a file for write only. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file|
|a||Open a file for write only. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist|
|x||Creates a new file for write only. Returns FALSE and an error if file already exists|
|r+||Open a file for read/write. File pointer starts at the beginning of the file|
|w+||Open a file for read/write. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file|
|a+||Open a file for read/write. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist|
|x+||Creates a new file for read/write. Returns FALSE and an error if file already exists|
Reading files will allow you to retrieve data from manipulation. 0:00 But what if you want to share this information with others? 0:03 Writing files will allow us to save any changes we make and 0:07 also allow us to change settings and update application details. 0:11 Building upon the same methods we use for 0:15 reading files, lets take a look at a couple different ways to write files. 0:17 We're going to combine our states into a single file. 0:23 Let's start with the new file named combine.php. 0:26 We'll start by opening a new file for writing line by line. 0:39 Once again, we use fopen. 0:44 We'll call this data/html/combined.html. 0:49 This time for the mode, we use w for write. 0:57 This tells a function to open the file for writing only. 1:02 It places the file pointer at the beginning of the file and 1:05 truncates the file to zero length. 1:08 This has the effect of erasing the file, so be careful when you use it. 1:11 If the file does not exist, it will attempt to create it. 1:16 Again, I'll surround the statement with the conditional, so 1:20 we don't try to write to a file we can't access. 1:23 Then within the conditional, I'll close the file. 1:33 We then start writing to the file using the fwrite function. 1:43 Like with fgets we pass the handler that we used in the fopen statement. 1:48 This time, we also passed the string we want to write. 1:55 I want to start with the original states file. 1:59 So I can use file_ get_ contents, 2:02 data/html/states.html. 2:08 Next, I want to write the territories within an opt group. 2:14 Again, I can use the file_get_contents, and 2:54 this time I'll use data/html/territories.html. 3:00 Now, do the same thing for the Armed Forces. 3:07 We already finished with fclose. 3:30 So, let's add a select and include the file we just wrote so 3:33 that we can check this out in the browser. 3:36 Great, now we have a single file to use first state dropdown. 4:10 If you view the source, you can see that it's a little hard to read 4:18 because we don't have proper line breaks. 4:23 Let's go back and add those. 4:27 A good way to add line breaks to a file is with the predefined constant PHP_EOL. 4:29 This will add the correct end of line symbol for 4:35 the platform where the script is running. 4:38 We'll add this before all but the first write. 4:45 Now, we may refresh the source. 5:15 Our line breaks show up nicely making the file easier to read. 5:16 Let's go back to work spaces and look at writing the entire file at once. 5:21
You need to sign up for Treehouse in order to download course files.Sign up