1
00:00:00,170 --> 00:00:03,920
Reading files will allow you to
retrieve data from manipulation.

2
00:00:03,920 --> 00:00:07,450
But what if you want to share
this information with others?

3
00:00:07,450 --> 00:00:11,400
Writing files will allow us to
save any changes we make and

4
00:00:11,400 --> 00:00:14,900
also allow us to change settings and
update application details.

5
00:00:15,950 --> 00:00:17,880
Building upon the same methods we use for

6
00:00:17,880 --> 00:00:22,240
reading files, lets take a look at
a couple different ways to write files.

7
00:00:23,250 --> 00:00:26,510
We're going to combine our
states into a single file.

8
00:00:26,510 --> 00:00:30,567
Let's start with the new
file named combine.php.

9
00:00:39,851 --> 00:00:44,450
We'll start by opening a new file for
writing line by line.

10
00:00:44,450 --> 00:00:46,555
Once again, we use fopen.

11
00:00:49,110 --> 00:00:57,310
We'll call this data/html/combined.html.

12
00:00:57,310 --> 00:01:02,090
This time for the mode,
we use w for write.

13
00:01:02,090 --> 00:01:05,710
This tells a function to open the file for
writing only.

14
00:01:05,710 --> 00:01:08,990
It places the file pointer at
the beginning of the file and

15
00:01:08,990 --> 00:01:11,850
truncates the file to zero length.

16
00:01:11,850 --> 00:01:16,150
This has the effect of erasing the file,
so be careful when you use it.

17
00:01:16,150 --> 00:01:20,040
If the file does not exist,
it will attempt to create it.

18
00:01:20,040 --> 00:01:23,529
Again, I'll surround the statement
with the conditional, so

19
00:01:23,529 --> 00:01:26,184
we don't try to write to
a file we can't access.

20
00:01:33,880 --> 00:01:36,571
Then within the conditional,
I'll close the file.

21
00:01:43,532 --> 00:01:46,940
We then start writing to the file
using the fwrite function.

22
00:01:48,950 --> 00:01:53,350
Like with fgets we pass the handler
that we used in the fopen statement.

23
00:01:55,050 --> 00:01:58,210
This time, we also passed
the string we want to write.

24
00:01:59,400 --> 00:02:02,050
I want to start with
the original states file.

25
00:02:02,050 --> 00:02:08,016
So I can use file_ get_ contents,

26
00:02:08,016 --> 00:02:14,394
data/html/states.html.

27
00:02:14,394 --> 00:02:19,480
Next, I want to write
the territories within an opt group.

28
00:02:54,438 --> 00:03:00,676
Again, I can use the file_get_contents,
and

29
00:03:00,676 --> 00:03:07,872
this time I'll use
data/html/territories.html.

30
00:03:07,872 --> 00:03:10,715
Now, do the same thing for
the Armed Forces.

31
00:03:30,822 --> 00:03:33,130
We already finished with fclose.

32
00:03:33,130 --> 00:03:36,202
So, let's add a select and
include the file we just wrote so

33
00:03:36,202 --> 00:03:38,361
that we can check this out in the browser.

34
00:04:10,780 --> 00:04:14,219
Great, now we have a single file
to use first state dropdown.

35
00:04:18,375 --> 00:04:23,174
If you view the source, you can see
that it's a little hard to read

36
00:04:23,174 --> 00:04:27,030
because we don't have proper line breaks.

37
00:04:27,030 --> 00:04:28,350
Let's go back and add those.

38
00:04:29,700 --> 00:04:35,565
A good way to add line breaks to a file
is with the predefined constant PHP_EOL.

39
00:04:35,565 --> 00:04:38,891
This will add the correct
end of line symbol for

40
00:04:38,891 --> 00:04:41,971
the platform where the script is running.

41
00:04:45,360 --> 00:04:47,977
We'll add this before all but
the first write.

42
00:05:15,086 --> 00:05:16,750
Now, we may refresh the source.

43
00:05:16,750 --> 00:05:21,685
Our line breaks show up nicely
making the file easier to read.

44
00:05:21,685 --> 00:05:25,155
Let's go back to work spaces and
look at writing the entire file at once.

