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.1 File Writing Basics

In the previous lesson, you learned the basics of what it takes to write to a file. There are a few steps that can seem rather tiresome after a while. The nice thing is that PHP has several other functions that wrap some of these common functions into one. Let's take a look at one of those now.

3.1 File Writing Basics

Now, let's start to work with writing to files. So before we actually start writing to files let's go ahead and think a moment, and discuss what it actually takes to write to a file just generally speaking in an operating system usually and then we'll talk about how we handle those operations in PHP. So, the first thing that you need to think about is what's the name of the file that you wanna write to and where's it located. Is it located in the same directory that you're in? Is it somewhere else on your hard drive? Where is it and what is it called. The second thing that you need to think about is, how do we open that file? We need to open that file or create the file if it doesn't exist. Then we wanna open that file to get ready to do something with it either write to it ,or read from it or append to the end of it. I mean, all these types are different operations we can absolutely do. So once we figure that out, what text do you want to write to that file? How do you want to format that text? What it is gonna look like? And you need to think about these things now because later on when you wanna read from it, you wanna make sure that you know the format that the data is in so that when you read from it, you can do that successfully. And you know what it's gonna look like and you can expect consistent results. So then once we have that, we need to actually write to the file. So, how do we physically write to the file, get the data that we want in that file and save it? And then once we're done with it, we need to close the file. And really that's an etiquette thing because if I wrote an application, or if I did something where I opened up a file, I get what's known as a handle to that file. I get kind of a pointer to that file, and I begin to lock it is kind of the terminology. So if somebody else came in and wanted to write to that file or do something to it and attempted to do that, it would say no, sorry. You can't do that, your operating system would typically stop them and say hey, somebody else is using that file, you can't perform this operation. So if you've ever seen those windows pop up, especially on Windows that's a common one, and it would say some other application has a handle to this file or something like that. So maybe we can't delete it or something like that. That's what's going on when that happens. So, how do we do all these things in PHP? Well, it's pretty simple. There's operations and even some functions that will take care of each one of these steps. So the first thing that we wanna do is we want to specify what this file is and where it is as far as our application is concerned. So let's create a file name, in this case, I'm just gonna call this dreams. Now by the fact that I'm just leaving this as just dreams, it's gonna create this file inside the current directory that I am in which just so happens to be the PHP file handling directory. Now, I could put on here a relative path. I could say go back a directory and then put it in app, or something like that. You could definitely do that. But in this case, I'm just gonna leave it as in my current directory cuz well, it's gonna make it easier for me to find it later on. So now that I have my file name, I need to open the file. And actually, if it doesn't already exist, I wanna create it. Well luckily enough that a lot of these operations are gonna be condensed into a single operation that'll be done for you. So now I can say my file is gonna be equal to fopen. Now fopen is gonna take a couple of parameters, and what this is going to do is say, I want to know the file name. What file name are you trying to open? So I'm gonna pass in file name, and then I need to pass in a mode. And, so, what exactly is a mode? Well, a mode is how you're going to open this file and what you're going to do with it once it's open. So I can open this in read mode, in write mode, in append mode, and other combinations thereof. So if I wanted to do it in read mode, which we'll see later on, I could specify an r. If I wanted to open it in write mode, I could do a w, in append mode, which means I'm opening it for write mode. But it's going to open and write to the end of the file. And then you can do other combinations. I could say read+ which means read and write. I could do write+, which once again means write and read. And I could do a+ which is the same basic thing where I could append and I could also read from a file if I wanted to. So really what I wanna be able to do in this particular application is I wanna open a file and I want to be able to write to that file a single line every time and on each line is gonna be a combination of my name and the dream. And with a little bit of formatting and then we'll talk about that in just a moment. So now that we have our file open and we kinda have this handled to that file, I wanna be able to write to it. So we're gonna say fwrite but before we can write to it we need to know what we wanna write. So let's start by creating another variable here called text. Now let's do a little bit of formatting here. So I need to know what my text on each line is gonna look like so when I read it later on, I can make sure that I decipher it properly. So let's do a little bit of delineation here. So we're gonna start with the name and then I wanna put that person's dream on that same line. So I'm gonna put a little bit of a delimiter here, let's say a double colon. So in between the name and the dream I'm gonna put a double colon so that later on I know that that dream is associated with that person and I can split them apart easily because I know that they're delimited by double colon. So let's also put in here the dream. Now I could just write this and we could go through the process of doing that. But one thing that I wanna warn you about is that when I'm opening this file to append, it's gonna look at the last place that was written at the end of the file. So it's gonna look for, If I put in there Derrick wants to each millions and that's on that first line, where it ended was on the end of that first line. So when I close the file and then I'll come back and open it later to append and it's going to see the last place that was written was at the tail end of that first line. So if I try to write it again, it's going to write on the tail end of that first line again. Which means every time I open this file and write to it, it's gonna just create one long line that's gonna be very difficult to read and you could do that but it would take a little bit more effort to read and I don't really wanna do that. So, the trick to this is that I also wanna put one more thing on here, and that's what known as the new line character. And the new line character is a \n. What that means is when I write that to a file, it's not actually gonna show up in most text editors, although some it will but you're not really gonna see that. This is just a marker for your text file to know that I want you to write this line, and then drop down to the next line, and get ready to write from that point on. So now, I've got that taken care of. I want to write this out every single time I call this function. And now I wanna actually do the write, so I wanna use the fwrite function. And I need to pass in my resource handle, which is just the file that I have either created and/or open. And then the string that I wanna write, and I wanna write my text out to that file. And once I'm done with it I'm simply going to close the file just like that, okay. So those are those operations that I talked about before in terms of PHP. So we have a couple of functions that we can use and that's pretty nice. But it still is several operations here and it's kind of painful. It's a little bit of a pain, but it works, and I think it's very important for you to understand the operations that take place to write data to a file, to read data from a file before we start using short cuts functions. So you really say kind of have an appreciation for what's going on behind the scenes. So let's go a head and save that and we'll open up per application again. Let's go ahead and refresh it just to make sure everything is okay. And let's go ahead and start to put some data in here. So we'll say, Derek wants to teach millions, and we'll go ahead and send our dream. And then let's say maybe Julie, I spelled Julie wrong. Julie wants to run a marathon. We'll go ahead and save that. Now, let's go back into Visual Studio Code and take a look at this. Now, if this was done properly and everything is working and set up correctly in the same directory that you are working in, the same directory as your index.php file, you should see another file called dreams.txt or whatever you named it within your application. And as you can see here we have these combinations. We have Derek wants to teach millions. Julie wants to run a marathon, and you can see there's an empty line here. And the reason that there's an empty line there is because when we were writing each line, there is a special character at the tail end of this that says, go down to the next line. That's that new line character which once again you can't see. And it just drops you down to the next line ready to input the next value. So we can go ahead and do one more if we wanted to. So let's close that out and will say go back in here. And will say that John, his dream is to learn to cook. And then will go ahead and send that in. We come back over to Visual Studio Code, open up our dream.txt file again and we'll see that John wants to learn how to cook. And once again with that new line there. So there you have it. We have the basic mechanism to be able to create a PHP file, take in some data from a form post, and then save that data to a file in this process right here. Now that all works, so that's fine and good but it is a little bit of work and you would have to write this whenever you wanna do this process. But there is a shortcut, but like I said before, I wanted you to learn what the process is so that when you lean the shortcuts, it's actually a little bit easier and you can have an appreciation for what's going on behind the scenes. So in the next lesson, let's learn a shortcut to be able to wrap a lot of this functionality into a single function, so we can shorten our code up just a little bit.

Back to the top