Unlimited WordPress themes, graphics, videos & courses! Unlimited asset downloads! From $16.50/m
Practice php and learn files 400x277
  • Overview
  • Transcript

3.2 File Writing Shortcut

Let's just come right out and say it. You should never trust the data that is sent from the user to your application. It is your job as a developer to make sure that your application continues to run successfully and to keep you, your company, and your users safe from both themselves and possibly malicious users. While this topic in itself can be studied and practiced for months, there are a few simple and common things you can do to make sure you are on the right track.

3.2 File Writing Shortcut

So in the previous lesson, we learned how we can actually write some contents, some strings, or some data to a file and it took a few operations here. And we wanna learn how we can take those same operations and kind of condense them a little bit, and use maybe a different function so that we can accomplish the same task- with fewer lines of code and we can absolutely do that. So let's take a look at what functions we're using here. We're using fopen so that we can open a particular file for a particular reason. And then we do an fwrite so we can actually write to that file and then we do an fclose so we can close that file, and not only we're doing an open but we're opening it for a pending. So, it would be nice if we could take those three operations, those three functions, and condense them, and it just so happens that we can. So if I were to get rid of these three lines, and just kinda remember what they do, let's see what we can do in their place. So what do we wanna do? We wanna be able to write to a file, and we wanna be able to append to that file. And we want to take care of the opening and the closing, and all that kind of stuff and not have to worry about it, so that it can just be done and we can move on. Well, it just so happens that there is another function, called file_put_contents. Now file_put_contents does basically something very similar to all three of those. It's going to check to make sure if that file exists. And if it does not, it will create it. It will open it for writing and you can also coerce this function to open it for appending as well. And then you can send some data to it, and then it will also close it as well. So let's go ahead and see how this works. Well, as you can see here, file put contents wants to know our file name. Well, we already have that, so we'll say file name. Then it wants to know the data, what is it exactly we wanna write to this particular file? Well, we already know that too, we wanna write text. And then it has some other things in here. It says int flags, and what are the flags? Well, there's a couple of flags that you can use to pass in here that basically works in a somewhat similar fashion to the mode in the previous example where we were using fopen. There's a few other things that you can do in there, but the nice thing here is what we're gonna use it for, is to say, I want to open this file. And I wanna be able to write to it, but I wanna be able to write to it with the FILE_APPEND flag. And you can concatenate, there's a couple other options you can pass in here, do you wanna lock it, do you not wanna lock it. And you can do all of that together by simply using the pipes. So if I wanted to put multiple flags in there, I absolutely could. So one thing that you can definitely do to learn more about this function and more of the flags, and different ways to use it is you could simply do a little research and look up the file_put_contents function. And take a look at all the different operations and parameters that you can ultimately pass things into. So let's go ahead and put a semicolon there. Let's go ahead and save this. Now that we've made that change, we've gotten rid of 3 lines and just replaced them with 1. And if I were to come back into my application, and go ahead and let's refresh this thing to make sure everything looks okay. And let's take a look and see what our file looks like to this point. We have Derek, Julie, and John. Let's come back into our application, and let's say Katie wants to start a company. And then we'll go ahead and put a period, and let's go ahead and send that. All right, so now it seems to have disappeared previously like the other ones have. So let's come back into our final, and as you can see here, Katie wants to start a company. So there you have it. Now, we know that there's a particular operation or series of operations that we need to be able to do to write to a file. Namely, opening a file, opening it for a specific purpose, whether it's reading, writing, or appending. We wanna be able to write contents, or some sort of string or something to that file, and then we wanna be able to close it. And instead of having to write those things out manually every single time, we can simply use the file_put_contents and then be able to handle the same operations. Now, you can use this in several different ways as well, you don't have to put in the FILE_APPEND flag. If you were to leave that off, then every single time you were to write this or use this function, it would actually overwrite the entire file with whatever your passing in as that value, that data that you're passing in. But like I said, in order for us to mimic the operations that we were doing in the previous lesson, we wanna use that FILE_ APPEND flag. So now we've been able to condense our code a little bit, we feel a little bit better. But at the same time, I know what's going on behind the scenes when we use the file_put_contents function. So that's pretty nice. So now, in the next lesson, we wanna move on to the reading style of the world. So now, I'm able to go into my application and write things to the dreams.txt file. But, I wanna be able to read from it too, without actually having to open the file up in a code editor or some other text editor, and have to read it. I want my application to be able do that as well. And so let's see what we need to do in order to accomplish that goal as well

Back to the top