Lessons: 12Length: 54 minutes

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

3.5 File Reading Basics

If you think that reading a file using PHP is anything like writing to a file in PHP, well... you would be right. The basic process is actually quite similar. We are going to start by checking to see if the file we want to read actually exists. Then we will open the file, read its contents, and then close the file. Let's get started.

3.5 File Reading Basics

All right, let's start by learning how to read data from files. So let's walk through the basic process first and then we'll start to talk about how you can do those things in PHP. So the first thing, once again you're gonna need to know where this file is? What it's called? Where it's located? All of that sort of good stuff. Then you're going to want to make sure that you check to make sure that file actually exists. Now when you're writing to a file, you can say, hey, I wanna write to this file, I wanna open this file and if it doesn't exist, create it for me. Well, on the read side, you're kind of making the assumption that it is there. So you're probably gonna wanna check to make sure that it actually exist first. If it does exist, then you're gonna want to open that file. You're gonna want to read the data from that file and then you're gonna want to close that file just like you've kind of done previously for the rights before. So let's go ahead and go through this process using PHP. So here we are in our read_dreams function and we're gonna leave that return there cuz I have a feeling we're gonna need that but we'll get to that in just a second. So the first thing that we wanna do is we wanna specify our file name just like we did before and we know that this is called dreams.txt. So let's start with that and then we wanna make sure that this file actually exists. So we'll say if file exists, so we have a nice function that will check that for us. And we can say that file name, so check to make sure that it exists and if it does then let's go ahead and open it. It's like we did before, we're gonna get a handle to that file. We'll say fopen and we've already learned that we want to take the file name and we want to specify a mode. And in this case I want to use read mode cuz all I'm gonna do in this case is read the contents from the file. And then I wanna be able to read that file. Well, there was an f right before, well you might think, let's use the f read and that's what exactly what we're gonna do. But let's take a look at this for a second. We need the resource handle. Well, we have that but then we need the length. Why do we need that? Well, fread is kinda like the Swiss army knife of read functions in PHP. Where we can use it to read all sorts of different types of files. Right now we want to read a text file, but you could also use this to read a binary file. So let's say I wanted to read the contents of an image file, well, that's not text, and you can just read the entire file. So we want to know how much of the file to actually read and that's where length comes in. Well luckily for us there is another function that we could use to get that. So we'll call this, size is going to be equal to file size. And we can pass in that file name, so go ahead and get the size, this will give you the full size of that file, and then we can pass this in as the lengths. So we want to read the entire thing and we wanna read it in to a variable, we'll call this file data. So now we have all the contents of that file and remember what's in that file? We have the combination of the name and the dream but we also have that new line that we put at the end of each line and what file read does is it doesn't discriminate between what's viewable to the end user and what's not viewable so it's gonna read in everything. So we have to kind of remember that as we go. So the next thing that we're gonna wanna do it we're gonna wanna close that file cuz we don't wanna keep it open, remember? So we're gonna go ahead and close it. So now that we've closed it, we want to return something to whatever is calling this function. Now remember, we are assuming that this is going to be in an array, but right now file data is basically just a long string of data. And it's a really big ugly string that has all of our data plus it has those new lines. So we wanna strip those out and just send an array, well how do we do that? We'll we've already kind of learned how to do that, by using this explode function where before we were trying to break it up on that double colon. But this time, we actually want to break it up on that new line. So let's come in here after our close and let's go ahead and do a return statement. So we'll say return, and we want to explode. What do we want to use as our delimiter? We wanna use our new line character. And what do we want to actually try to explode on? And that's gonna be our file data, just like that. So now we are going to check to make sure the file exists. We're gonna open it, and we're gonna get its file size, we're gonna actually read it, and then we're gonna close it. And then finally, we are going to return the exploded values exploded on that new line character. Now what happens if that file doesn't exist? Well, then we're gonna have an else block here, and that else block is simply going to return that empty string, cuz we know we're already checking for that. Down here in our code, so we'll just go ahead and leave that there. So let's go ahead and save this, and we'll come back over to our browser, let's go ahead and refresh this page. And we're gonna see now that we have Derek, Julie, John, and Katie and we have all of their dreams, which is actually pretty cool. Now this will absolutely work. Now what's that process again? Well, it's a little long winded. We have to check to make sure that the file exists, we're gonna open it, get the file size, read it, close it, explode it. Yada, yada, yada, so just like we talked about on the right side of the equation. There's a lot of operations here but it's very important for you to understand the things that are happening behind the scenes before you start jumping into shortcuts. So in the next lesson I'm gonna start to introduce some shortcuts and some other operations or some other functions that you can use to read the data and kind of condense some of this logic just a little bit.

Back to the top