 Hey everybody, this is Brian. Welcome to the 23rd LAMP tutorial. Today we're going to be discussing files, how to open a file, close a file, read a file, write a file. So what we're going to do here, and I've already run a test script here, but there's going to be a file we're going to call test.txt that we're going to write out into our web directory. And then we're going to read it back. So if you go to w3schools.com or whatever website you're learning PHP off of, you can read up on how to open a file. And you simply say fopen, and then the file name, and then the mode. Now what's a mode? Well, when you work with a file, there's multiple modes. There's read, write, and append. Now, as you can see from this list, there's more than read, write, and append, but those are the three basic ones. They have other modes as well, such as rplus is read slash write, starts at the beginning of the file. wplus is read, write, opens, and clears the contents of a file, creates a new file if it does not exist. So you've got to kind of read the descriptions of what each one of these does. For example, x is write only, creates a new file, returns faults, and an error if a file already exists. That's a real handy one if you don't want to overwrite a file on accident. Here's some examples of how to do that. You just say file equal fopen, and then you're reading it, exit on the upload file. That's what you do if you had a problem opening that file right here. How to close a file? You just call it fclose, and then you give it the variable. Notice how you're storing the file in a variable. Checking for the end of the file is pretty easy. You just use f-e-o-f, or end of file. And then they have a little example of how to read a file line by line. And we're going to go through and do this real quick here. So first thing we want to do is write a file. And we're going to say file equal fopen. And notice how it wants a file name and a mode. So we're just going to say test.txt. Now this is the relative path to your web application. So if you're not writing it root, you're actually writing it at the root of your website, where your website's hosted. So if you want it in a subfolder, you're going to have to put the full path. And then the mode, we just want to write. And whenever you open a file, you should close the file as well. So we're going to say fclose, and then give it the name of the file. I should say the variable, in this case $file. And now we're going to actually write contents out of that file. Oops. fputs is the function that does that. And notice how it wants a resource fp. Well, the resource is actually the file. And then it wants, what are we going to put out here? And we're just going to write out, actually, let's do the current time. So we'll say, oops, hour, minute, second. And ta-da. We wrote to the file. Let's go out to our home directory here. Sure enough, there's a test.txt. Open that up, and that has the current hour, minute, and second. So let's go back in the Komodo edit. I'm sorry, Komodo IDE. And now we're going to read the contents of a file. And we're just going to say file. Whoops. Here we say, oops, a lot. Mode, we want to read only. And whenever you open a file, be sure to close it. Now, we want to read this file line by line. You've probably seen fputs, so you're thinking fgets will do it, but we have to do it line by line. That's the tricky part here. So we're going to say, while, not, eof or end of file. So what we're saying is while we are not at the end of the file, and then we hand it the resource, which in this case is our variable file, then we're going to, you guessed it, get our resource here. So what we're doing every time we run this PHP file is we are writing the current date time, or actually the hour, minute, second, closing the file. And then we're opening the file back up in read mode. We could have very easily, you know, open this and read right, and then move to the beginning of the file. But I'm just for demonstration purposes. And then we're saying while we are not at the end of the file, remember this is going to start at the very beginning of the file, read through line by line, and then loop back here. And it's just going to keep doing that tour at the end, and then we're going to close the file. And we're just printing the lines out. So every time we run this, because we are overwriting that file, you see the seconds move. Pretty easy, pretty simple. Reasing and writing a file in PHP is fraught with error if you have special permission set up on your web directory. Sometimes you'll be able to read files but not write them, and that's where some people get into problems when they go to upload files. Well, pretty simple tutorial. I'm going to cut this one short. I want to touch a little bit more on files in the next tutorial.