 Hey everybody, this is Brian. Welcome to the 25th LAMP tutorial. Today we're going to be discussing how to upload a file and some of the problems that can arise. Alright, now I have to say working with file uploads in PHP is actually much simpler than in some languages. In some languages it's just a nightmare. But PHP makes it pretty simple. Here are some things you need to be aware of. When you upload from a form, your ink type must be multi-part form data. And what happens is, when you submit this form, it will create a global variable called dollar sign underscore files, or files. Notice how this is a multi-dimensional array. You can upload multiple files at a time, but for this specific tutorial we're going to upload just one file just to learn the principles of this. Notice the name here for the first element, file. Well, it gets this name from your input. See, name file. So just know that if you change this name, you're going to have to change it here to match your other code. I should say PHP will automatically do it. Now, what happens is when you submit your data, I should say you submit the form, the data is uploaded. The web server automatically hands it off to PHP that realizes, hey, they're uploading a file. So rather than forcing you to play around with the get, post, and request variables, it makes a new one called files. And populates it with this information, the file name, the type, this is a mime type, the size, the temp name, and the error. Now, the error is a number that correlates to the error code. If it's zero, that means everything worked fine. If it's something other than zero, it means something bad happened. Temp name, what's a temp name? Well, what happens in the background is that the file is actually uploaded to the server into a temp location based off the PHP config. From there, the file just sits there, ready for you to do whatever you want to do to it. So let's actually make this example work. Let's go out here. And of course, we're going to change this around. So we're just going to post into ourselves here, and we're going to grab that other code super quick. And don't worry, we're going to go over this line by line. I just want to get it nice and pretty in my little IDE here. All right, so what we're going to do here, as you can see, is we're posting into our current page, Tut 25 PHP. The ink type is multi-part form data. Once we hit the submit button, the data will be posted to the web server. PHP will detect, hey, they're uploading a file. It's going to make this global variable called files. So the first thing you need to do is check to see if the error field is greater than zero, because if it is, it means there was an error. That's why we're spitting out, hey, error, something happened. If it's less than zero or equal to zero, that means no error occurred. Then we're just going to display the statistics of the file here. Now let's do this. Now you see how it says icon two, it's because this is previously cached, but we're just going to grab a separate file here. Say icon one, submit, and voila, there's the information for icon one. There's our file name, there's our mime type. If you don't know what a mime type is, you have to think about things from a file level. Files are just blobs of data. The operating system has no idea the difference between a spreadsheet or a picture or a video or an mp3. It tries to determine that based off, well, the file extension. The problem is file extensions can be changed and they're misleading, so they include what's called a mime type. Different operating systems handle mime types differently, but essentially it tells you how to handle that file. We can see it's an image, a JPG, so we tell it, hey, this is an image. Use image programs, not spreadsheets. Here's the size in kilobytes, and here is the temp location. Now, that is the actual file itself that has been uploaded to the server, ready for us to manipulate and do as we wish. How do we go about displaying that on our web page? Well, that's a very good question. The first thing we need to do is put it somewhere where we can actually access it, so let's move the file. We're going to make a few variables here. We're just going to simplify this a little bit just so we can see what's going on here. Some of you out there that have seen some of my C++ videos are probably sitting there going now, Brian, and you're wasting memory. You already have access to those. Why are you recreating them? Well, just for simplicity's sake and for illustrative purposes. So we have our directory. We want to move it to our var www. We have our file name, and then we have the temp name where it's actually been uploaded to. Now, remember, the name is actually the name of the file, not the full path. Now, PHP has a function called move uploaded file, which, as you might have guessed, moves the uploaded file. It knows where that temporary path is and knows how to find it. All right. So what we need is a source file and a destination file. Well, our source file in this case is the temp file. And the destination file is going to be dir, and you guessed it, file name. It's worth noting that if you put it in a folder other than www, like if you make an images folder or something like that, that you're going to have to have the permission set up correctly to do that. Sometimes they propagate automatically, other times they don't, but it's beyond the scope of this tutorial. Right now we're just handling how to upload the file. Now we want to display that file. So we're just going to say echo, and we're going to repost that data. Oops, probably help if I didn't have an error message there. Oh, yes. That's what I get for being in a hurry. There's our file in all of its glory. So once again, let's kind of look at this real quick and see what's going on with this code. We have our form. The user can click here, and let's grab a different image. Once they submit this, it's posting back into itself using the ink type of multi-part form data. Notice how the input, the name, has to line up with what we're looking for here. So use file or uploaded file, however you do that. Always check the error. Just make sure the error number is zero. If it's greater than zero, then something happened, and you should let the user know. And then we're just grabbing some statistics. You don't really need to show those to the end user. And then, ta-da, displaying the file. We're moving the file using the move uploaded file, and that's very important. If you don't move the file, it's going to be in the temp directory, which your web server does not have access to. So let's go out to our file system here. Let's go far. Yeah, see, there's the file we just uploaded. Submit this. Notice that, ta-da, there's our new file. If we go out here, ta-da, there's our new file. Very simple, very easy to work with. There are many, many, many, many ways of doing this in PHP, so many that I don't think I could do enough tutorials on them. For example, on W3Schools, or as one of the viewers, I'm sorry, I forgot your name. I'm horrible with names, especially right on the spot. I don't remember, but it's not coming, I'm sorry. Here's a good example. He calls us W3Fools, not W3Schools. I thought that was pretty funny. But they do show a pretty good example of how to filter for extensions. For example, if, you know, files, file type, equals, et cetera, et cetera, then, blah, blah, blah. Important note that the, for IE to recognize JPEG files, they must be PJPEG, Firefox will be JPEG. And I think the reason why he calls them W3Fools is because their information is not 100% accurate, 100% of the time. I've worked with IE in the past, and I do know they do take MIME types of JPEG. So that's how that works. Let's see. Saving the uploaded file. They pretty much just talk about what we just did. Move uploaded file. They do a little more complex stuff. For example, they check to see if the file exists, which is always a good idea. If the file already exists, you know, displaying error message, otherwise move it. That's all for this tutorial. Pretty complex topic, but PHP makes it very simple. Trying to think if there was something else. Oh, yes, directory permissions. If you are in a real-world situation and you're trying to upload a file and you keep getting error messages, and let's say, you know, you have like a new folder. We'll just call it images. We'll go out and look at the properties of that. Because chances are, you might not have permission to actually move the file. See, access? You need create and delete. Things of that nature. It all depends on how your web server is set up and under what username your web server is running and PHP is running. So that's all for now. Thank you for watching. I hope you found this educational and entertaining.