Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
In this video, we demonstrate two of the four ways to read a file. Each of these approaches allows you to work with the files in a different way.
Download: File Handling Cheat Sheet
Available File Modes
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 |
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
If you've taken our basic PHP course or
0:00
really done almost anything in PHP,
you've already been reading files.
0:03
They include and require statements read
additional files into the current file.
0:08
This is a great way to cut back and
rewriting the same thing all the time.
0:13
We can write things once and
include them wherever we want.
0:18
But this course is really
about giving you control.
0:22
We want to be able to do
something with those files,
0:25
besides just dumping them
into the current file.
0:28
So, let's take a look at some of the other
options we have for reading files.
0:31
We're going to create a form,
and bring in country, and
0:36
state drop-downs from some other files.
0:39
If we click on the HTML countries.html,
and view the page source.
0:41
You can see that this is
a list of drop down options.
0:48
The other HTML files
are formatted the same way, and
0:51
you can browse each of them if you'd like.
0:54
Back in work spaces create a new
file name state_formed.php.
1:00
Let's add our form tag and
then create a country drop down.
1:09
<label for="country">Country</label> And
then, our select.
1:20
For country, we're just going
to include our countries file.
1:44
Let's view this in the browser.
1:59
We can see that their
drop down is populated.
2:11
Let's go back and add a state drop down
now using a little more control this time.
2:13
We'll add a label for our state.
2:23
And then our select.
2:31
This time we'll start by reading
the file with the fopen function.
2:47
This function opens
a connection to a file,
2:53
and returns a file handler that we
can use to connect to the file.
2:55
This is the same kind of thing that we do
when we set up a connection to a database.
3:00
To open a connection, we first tell it,
which file we want to open, and
3:04
then tell it which mode we want to use.
3:08
What are we doing with the file?
3:11
Reading?
3:13
Writing?
3:13
Appending?
3:14
Or even creating a file
if it doesn’t exist.
3:15
Check the notes associated with
this video for mode options, and
3:18
additional features of fopen.
3:22
First we pass data/ html/states.html.
3:24
And then, since we want to read the file,
we use r as the mode.
3:31
We'll wrap this in an F statement, so
3:36
we can perform our actions only if
we're actually able to open the file.
3:38
We set our file handle equal to the fopen
And now, we perform our actions.
3:47
The first action I want to add within the
statement is the action to close the file.
3:56
Use fclose and pass the file handler,
Before I close the file.
4:01
I want to loop over each line,
we can create any kind of loop we want.
4:09
We could loop through only
a specific number of lines, or
4:14
until we find a certain character.
4:18
I want to loop through all
the lines in this file.
4:20
So, I will use the end of file function.
4:23
While not file, and a file,
and our file handler.
4:27
To read each line,
we use that fgets function.
4:38
This returns the current line as a string,
and moves the pointer to the next line.
4:43
We can now echo that line.
4:51
Let's preview this in the browser again.
4:56
This time we see the state
drop down as well.
5:00
But we haven't actually done
anything more than include the file.
5:04
So, let's say, that I want to
default the selection to Oregon.
5:07
Let's go back to Workspaces,
and add some additional code.
5:11
If the string position
5:20
of line for Oregon.
5:25
This will check if my line
contains the string Oregon.
5:34
If so, then I want to string replace.
5:38
Oregon, with
5:43
selected Oregon.
5:48
Else, we'll echo the line.
6:06
Let's check our browser again.
6:10
This time Oregon is selected by default.
6:14
We could make any comparisons, or
manipulations we wanted to this line.
6:17
But let's check out another function for
reading files.
6:21
You need to sign up for Treehouse in order to download course files.
Sign up