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