Placeholder Image

Subtitles section Play video

  • So that's how about the file put contents function in PHP, as I've taught about PHP is essentially the secretary for the Internet.

  • It takes data from one location and stores it into another.

  • So it may take data for my form and it may take data from a database and may take data from a file mayday.

  • Take data from any number of locations and then it has to put that data somewhere.

  • So we've been printing data to the screen is what we've been doing so far essentially, with that print function, print out to the screen and then we see the data.

  • But the question is, what if we wanna make that data more permanent, especially if we don't wanna have to build our own sequel databases?

  • So a lot of times, people use my sequel in orders a store data, which is a great way to do it.

  • But then you actually have to spend upon my sequel database and go through all the configurations.

  • What if you want something a little bit simpler?

  • Maybe not the best security.

  • So it's not the best, best way from a security standpoint to store data, but just an easy way where you can dump sad it is that of someplace so that you could do something with it later.

  • Well, by writing toe a file, you can do that.

  • So essentially, what you can do is you can take your PHP variables.

  • You can write them to a new file, or you can upend an old file and therefore that information will be stored until you get around to deleting it.

  • So that's where the file put contents function comes in is you're able to take the variables and put them into a file.

  • So with that, let's go over to the computer so I can give you a demonstration on how this works because this is where we start to get into the nifty neat stuff I think with PHP and actually start to be able to do things that are really functional in the real world.

  • So this is a little script that it created to show you how to write toe a file using file put contents.

  • So we call this file put contents dot PHP and of course, we open it up with the PHP tag.

  • Now under that, we're going to have a number of different variables here.

  • And so the first variable that we're going to have is the file variable.

  • So this is going to be the name of the file that we're going to be writing Thio.

  • So it's important to understand that if this file does not currently exist, as long as your permissions aren't screwed up, this script will be able to create the file.

  • So it's important.

  • Understand?

  • The file does not have to exist before the script runs.

  • This will check to see if the file exists.

  • If it doesn't exist, it will create the file.

  • And so with here with that particular variable, we're just going to call a dollar sign file to make it easier equals single quotation marks and then whatever the name of the file is.

  • So if you want us to be t x t a t m html PHP again, you can you can have PHP, right?

  • Other script files that will run using something else.

  • Basically, this is just writing to some kind of asking file.

  • You give it whatever whatever ending you want.

  • So what we're doing here is single quotation.

  • Mark, we're going to call this file dot html.

  • I'm calling it dot html so I can bring this up in a Web browser and actually look at what the output is.

  • Ah, then we're closing the single quotation marks.

  • And then, of course, we're ending aligned with the semi colon.

  • The next thing that we're going to D'oh is with this particular script, it's kind of gonna be like a simulation of a log file.

  • And so what it's going to do is again, it's going to print out something it oration, and that is going to give a time stamp.

  • So it's gonna print something out, and then it's going to give a time stamp.

  • Now, in order to do that time stamp, I need to create a variable for the time stamp.

  • So we're going to do here is we're going to do dollar sign time.

  • So any time this is called to be printed out, we wanted to be what the current time is.

  • We're going to have a equal, and we're going to have it the time function.

  • So this is going to go.

  • It's going to look at what the current computer time is, and then it's going to set the value of time to be whatever currently is.

  • And then we close this with a semi colon.

  • Now what we're going to do is we're going to create the variable information.

  • So this is the variable that is going to get printed in to file.

  • So your file and then you have the variable game printed into files.

  • We could just call this dollar sign info, and this is going to equal double quotation marks.

  • And so what we're going to say, Is it aeration?

  • So we're just going to print out it oration space, and then whatever the time is, and then since we're going to be looking at this with a Web browser, then we're going to do a break.

  • So we're just going to say generation space and whatever the time is, break next line, Next line, next line next line, because what we're going to do with this particular files we're going to upend.

  • So that's going to add on to the end of the file again.

  • Think of it like a log file.

  • Whenever something happens, you have whatever the comment is, what everything that happened, and then you have a time stamp so and then the break said.

  • Then we do double quotation marks to end that.

  • And then, of course, we do the semi colon.

  • And now this is where we get to the function.

  • So this is where we're going to write the dot info variable into file.

  • So we do file underscore, put underscore contents.

  • Then we open the parentheses.

  • The first thing that we do is we feed it the file name.

  • What is a file that we're printing too?

  • So we're going to give a dollar sign file, then we're going to do comma.

  • And then the next thing that we're going to do is say, What are we going to be writing into that file?

  • And so we're just going to feed it Dollar sign info.

  • So what we're going to feed into the file is this particular line, and then we're going to do comma.

  • And so, for this particular project that we're doing, we're going to do file upend.

  • So if you don't add upend here, it will overwrite.

  • So again, this important thing, especially with troubleshooting, I would argue in the beginning, you always do upend, so therefore you always know whether or not it's actually running because if you overwrite and you screw up the overwrite, you may get some weird errors.

  • So by a pending, what will happen is this will continuously keep adding to that file.

  • And so you can see the print previous iterations that have happened and you can see what you're currently doing.

  • So I would argue at this point you just do file upend, and then you close the parentheses and then you do the semi colon.

  • Well, the important thing to understand about this particular script is that this script is going to run on one page.

  • So file put contents dot PHP is going to be one page that we're gonna open up with the Web browser and then it's going to output the information to file dot html.

  • But again, from a troubleshooting perspective, one of the questions that you have to ask yourself is, how do you know whether or not this script actually ran?

  • So if all we do, if we stop at this line, we don't add this additional line, we just stop here.

  • What will happen is the script will run and all we will be left with is a blank screen will just simply be a white screen because nothing is being printed out onto the file.

  • Put contents dot PHP screen.

  • It's all getting printed out to that file dot html file.

  • So what I'm going to do here just as a troubleshooting thing to show that the script actually ran so it ran.

  • If something isn't printed to file dot html, maybe there are other issues with it, but I want to make sure that the script actually ran.

  • What I'm going to dio and this is going to show up on the file put contents dot PHP pages.

  • I'm going to do print that double quotation marks and what I'm going to say.

  • It's script ran at Colon, and then whatever the value of time is, so this.

  • This will get printed on the file put contents dot PHP page so that I can see that the script ram and then I can go to a file dot html page.

  • And if something isn't working there, at least I know that the script ram, if you understand what I'm saying.

  • So this is an important concept, especially when you're new is building in these little troubleshooting routines so that you verify.

  • Okay, The variable is what I think the variable is.

  • Yes, this particular script ran.

  • And then what you do in the real world is you can add these things in, verify everything, works and then you can go through and start deleting or commenting out thes particular lines of script when you know everything works, how it supposed to, But that's all we're going to do.

  • Here is just to say that the script actually ran, so this has all been uploaded.

  • And so what we can do is we can come here.

  • We go to silicon, does oh dot com and then we're going to file put contents.

  • Actually, before that what we can dio is we go to silicon does show dot com, and we could do file dot html and what you'll notice here is file dot html does not currently exist within the Web server.

  • And so what it's doing is it's defaulting me back to what the default home pages right now, so file dot html does not actually exist.

  • What will happen as soon as we press enter here.

  • Now what we can see ISS script ran at and This is the computer time, so I can see.

  • Okay, the script ran at this time, so I know the script itself fought fired.

  • What?

  • Whether whatever happens with filed out html But then we can come over here and I could do Refresh.

  • OK, and now I can see so the it oration and we can see the time.

  • So when I did this, the script ran at this time and at that time it printed out the information here.

  • So now I know it works again.

  • What I could Dio is this Since we're a pending with this particular script, I can just simply refresh so I can refresh and see how c how that number ticks up.

  • Keep sticking up.

  • Well, then I come over here and then I refresh.

  • And now we see we have all of these different iterations.

  • So imagine every time I'm refreshing.

  • This is an event happening within your server.

  • And so that's triggering that file put contents function to add to file dot html and then we can come here and we can look at that log file.

  • So this is where you might you might print, You know who logged in At what time?

  • Who logged in with what I p address?

  • At what time?

  • You know any kind of different things.

  • The idea here is that this this is how you can create a basic log file or anything else.

  • Ad again.

  • Be able to write to a file.

  • So this is all it is again.

  • What you do the first thing that you do is you create the variable for the file and you give it whatever file that you're gonna be writing to single quotation marks.

  • This is just an additional variable for me.

  • I'm just creating that variable for time so that I that I have something to troubleshoot with, then passed that we're going to do is we're going to create the info variable.

  • So this is what's going get printed into the file itself years where we create it again, adding that time variable.

  • And then all you do is file put contents, parentheses, the file, the variable for the file, the variable for the information.

  • And then again, I would argue for you guys to do the file underscore upend just because it'll make your life easier while you're trouble shooting At least in the beginning and then go from there this again has to be all in upper case.

  • Just make sure you do this all in uppercase, close parentheses and semi colon.

  • Then just to make sure your script is actually running, I would argue you should have something run on the page itself.

  • But that's your your decision for me.

  • I just did.

  • This script ran at time, so you know what happens.

  • So this is how you use the file put contents to write to a file.

  • And, ah, it's pretty simple script.

  • All in all, So that's really all there is the file underscore Put contents function in Ph.

  • B.

  • This is an easy way for you to be able to write to a file.

  • And again, this gives you a way to store data that's coming in from locations again if you have a form.

  • So if you're having people fill out a form, this is an easy way to dump the information from that form into a file with again.

  • And it gives you a way to access that information without having having my sequel database and whether not having to do sequel statements in the whole nine yards.

  • Now here's one of the warnings.

  • Warning, warning.

  • Warning.

  • Will Robinson.

  • One of the reasons again coders a PHP is because so easy to ride in PHP that a lot of new coders do idiotic things when it comes to security.

  • Now, do you remember where this you are writing to a file?

  • You're most likely right into a text file.

  • And most likely, if you're watching this particular Siri's, you probably don't know how file permissions work on your server.

  • Why this matters is, if you're going to be writing to files, I would argue you shouldn't ask for things such as people's email addresses or anything that may come close to being private.

  • Because, remember, this is a text file, so this is very easy to read.

  • If you put information into a database, you have to use sequel statements.

  • In order to pull that information out, you have to have user names.

  • You have to have passwords.

  • It's not that sequel can't be hacked, but there's a couple of steps that you have to go through.

  • If you have a dot t x t file.

  • Basically, anybody with a computer can read it.

  • If you screw up on the permissions and it's readable by the public.

  • Um, if if you have again, if your company, if you have people put in their first name, last name, email, address and phone number, you dump that into a text file that's readable from the outside world.

  • That's when nasty security problems happy and happen, and people start getting spear fish and the rest of it.

  • So with this particular thing, it's not.

  • It's not that you can't use files as a secure way to store data.

  • It's just probably not the best way to do it.

  • And unless you really understand what you're doing, I would say that that's not what you should do.

  • So this is good for things.

  • Again, let's say you had a sign up list so you have a sign up list for an event, and you just want people to sign in.

  • So basically people on go, they can put their name, it dumps their name and that's it.

  • That's pretty secure.

  • Uh, if you want to know something like T shirt size, so let's say you have a company or organization and you want to get people T shirts for that company again.

  • Their name, their T shirt size, that's that's a good way to store information again.

  • Log files do be careful with the log files.

  • If you start dumping in too many, I p addresses too much information that that might be confidential.

  • Then again, you could have problems if a hacker is able to view those log files and then realized, Oh, this is your active directory server and this is your exchange server and this is your other server, and this is who logs in so on and so forth.

  • So do be careful with the log files, too.

  • But that's really all there is to writing to a file again.

  • This is a very simple concept where it gets complicated and is in the real world, where you have to decide what's appropriate to write to a file, what's not appropriate to write to a file.

  • And then again, once you wrote right to a file, how are you going to interact with that data again?

  • This comes back to the idea of how do you want to print data to a file?

  • Do you wanna print data that's viewable by a Web browser or print data that's viewable by a text editor.

  • So if you're using, you're gonna be viewing the file on HTML.

  • Then you would use break in HTML in order to break to a new line.

  • If you're going to be viewing this information, let's say using a text editor just work process or something you won't do that slash in for a new line.

  • So this is the type of thing that you have to think about when you start using this.

  • But overall is just one of those things.

  • It just takes a little bit of experience.

  • So with that, that's the file underscore put underscore contents, function and PHP, and that's why it matters.

So that's how about the file put contents function in PHP, as I've taught about PHP is essentially the secretary for the Internet.

Subtitles and vocabulary

Click the word to look it up Click the word to find further inforamtion about it