7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

3.6 File Reading Shortcuts

Just as there are shortcut functions for writing data to files, there are also shortcut functions for reading data from files. In this lesson, we will discuss two of these functions. One will read the entire contents of the file and give you back a single string. The other function will read the file and give you the contents back in an array. Which one you use is up to you.

3.6 File Reading Shortcuts

So let's take a few moments to adjust our code here just a little bit and see what other sort of options we have when it comes to writing to a file. So this process will work. We can open the file, get the file size, read the file, and then close it. But I'd like to condense these operations just like I did for the right operation. And the way that we're gonna do that is by condensing all of these down to a single line where I can get my file data. And if you recall when we were writing data to a file, we were using the file_put_contents function. But in this case, we wanna use a different one called file file_get_contents, and it just so happens that this is quite easy. All I have to do is pass in my file name and there is a couple other options you could pass in here. You could pass in an offset, meaning where do you wanna start reading the file from. Yeah, by default it's a zero, so it'll start reading from the beginning. But you don't have to read from the beginning. You could read from somewhere else as well. And you can also specify the max length, meaning how much of the file do you actually want to read. You don't have to read the entire file, which is kinda nice. But in this case, we're gonna leave the defaults and that will give us the entire file. Now one thing to know about file_get_contents is it's going to read the entire file in one shot. Meaning what's coming out of this is going to be a string that's gonna contain everything in that file, including our new lines. So if we wanna switch over to this function, we just have to leave our explode in here and continue to break on this new line. So let's go ahead and save this. We'll go back into Chrome and refresh. And we'll see that everything continues to work, which is really good. But we can actually take this a step farther. So instead of having to read everything in a single line, which, once again, if that's what you wanna do you can absolutely do it, we can switch this up to another function called simply file. In this case, it's going to take in the file name again. And you can pass in some flags in a context. And the reason that this is so nice and so different is that this is actually gonna read every line of the file into its own entry in an array. So the resulting value that's in file_data is actually gonna be the array that we want. So that's kinda nice. So we don't necessarily have to use this explode. But the problem there is that it's going to leave all of those new line characters on the end of each line that it sticks into that array, and we don't necessarily want that. But lucky for us, there is another parameter in here for the file function for flags. And we can pass in a specific flag called FILE_IGNORE_NEW_LINES, where every time it comes in and reads those new lines, it's actually gonna strip them off of the result. So now, because of that, I no longer have to use this explode function. I can simply strip it off the end here and just return the file_data. So let's go ahead and save that. Go back into Chrome and refresh. And it continues to work the way it did before. So there you have it. There's three different ways of being able to read a file. You can use all the operations separately or you can use file_get_contents, knowing that it's gonna read the entire file or how much of the file you want. But it's gonna read everything into a single resulting string, which, depending on what you're trying to do, could work just fine. Or you can use the file function, and the file function is going to read the entire file expecting there to be multiple lines. And each line of the file is going to be it's own record in an array. So that's kind of a nice compromise, depending on what it is you're trying to do when you're reading the files.

Back to the top