‫All right guys.
‫So now we're going to look at the file superglobal which is really helpful in uploading files.
‫So what we have here is basically the same code from the post Superglobal video where we have title
‫description submitted.
‫We're checking to see if it's a Post request.
‫And if the file the form was submitted.
‫Then we're getting the title and the description and setting submitted to true.
‫And then down here, if it was submitted, we show the title and description.
‫Now the only thing that I've added to this is the file input.
‫So this div right here, which has an input with the type of file and the name of logo okay.
‫So the name is really important here.
‫So let's start off with just going into this if statement.
‫And I'm just going to echo out some pre tags because I want to do a VAR dump.
‫But I want it to to be readable.
‫So I'm just going to do that.
‫And then we'll do a var dump.
‫Of just the files.
‫Superglobal.
‫All right.
‫Don't forget the underscore.
‫And then let's go ahead and just do a quick reload.
‫And I'm going to choose a file.
‫So I have the thumbnail from one of my last YouTube videos.
‫And you can put whatever you you don't even need to fill that out.
‫But let's hit Create listing.
‫And now you'll see that what we get from this file, superglobal is an array that has one item, right?
‫And it has a key of logo because that's what I gave the the file input.
‫That's what I gave as the name was logo.
‫So that's going to be the file input.
‫The file input name.
‫Then that is an array with six items.
‫The name okay.
‫So the the the image or file name, it doesn't have to be an image.
‫And then we have the full path which in this case is just the the file name as well the type.
‫It's a Jpeg.
‫So it's going to be image slash Jpeg, the temp name, which is going to be something weird like this
‫error which we don't have any.
‫And then the size which is going to be in bytes okay.
‫So that's the data that we can get.
‫And you can access any of those directly.
‫So if I do like files and then the name of the image which is logo and then the field that I want like
‫name.
‫So I can do that as well.
‫If I just reload, it'll resubmit.
‫And now I have the name.
‫Now it didn't upload or anything like that, but we're able to to get that data.
‫So what I'm going to do next is let's see.
‫We'll get rid of these pre tags.
‫And I want to set just files logo to a variable.
‫So let's get rid of name.
‫Oops.
‫And then let's.
‫Get rid of echo and let's say file equals file logo.
‫Now we can access for instance we could say file and then name or whatever whatever field we want to
‫access.
‫So the first thing I want to do is check for an error.
‫So remember we have that error value.
‫So let's say if.
‫And I'm going to say if the file.
‫Error and we can check to see if that equals upload error.
‫Okay.
‫Meaning that there is no error.
‫Then let's just let's just echo success.
‫Just to make sure that that that works.
‫So if I reload the page resubmit the form I get success.
‫All right.
‫So in here I want to specify the upload directory.
‫So where oops that's not what I want.
‫So upload directory.
‫Let me just put a comment here and we'll say specify where to upload.
‫And I want it to go in a file sorry a folder called uploads.
‫And I'm going to put the slash here and you'll see why in a few minutes.
‫So we have our upload directory.
‫Then we want to check to see if that exists.
‫And if it doesn't we want to create it so that we don't have to manually create it.
‫So let's say if not is underscore dir which does just what it sounds like.
‫It checks to see if there's a directory with whatever we pass in here, which will be the upload directory.
‫And then if it's not there then let's call mkdir which will make a new directory.
‫And we're going to pass in upload dir.
‫You can also pass in the permissions which I'm just going to set to 0755.
‫And then if it's recursive we'll set that to true.
‫So that will actually create the upload folder.
‫Next thing we want to do is create a unique file name.
‫Let's say create file name.
‫And here we're just going to check and create directory.
‫Now for the file name.
‫You don't want to keep the default file name, which you can get with with this, right?
‫You can say file name like that.
‫But let's say that you're uploading an avatar for a user profile and you upload a file called avatar
‫Jpeg.
‫Chances are someone else is going to have a file called that, and then you're going to have problems.
‫So what we can do here and there's so many different ways to do this, but I like to use the unique
‫ID function which will generate, as you can see, identifiers simultaneously on several hosts that
‫might happen to generate the identifier at the same time or at the same microsecond.
‫So this works really well.
‫Now in addition to this unique ID, I'm going to add okay, I'm going to concatenate a hyphen.
‫And then I'm going to concatenate the the default file name which we can get like that.
‫So it'll still have the file name as part of the file.
‫You know the old file name is part of the new one, but it will also have this unique ID.
‫And I want to set this to file name.
‫Sorry.
‫So let's just go ahead and echo file name just to make sure that all this works.
‫What it should do is create the uploads folder and it should echo out the new file name.
‫So I'm going to reload resubmit.
‫And you can see that it's showing the file name which still has the original in it, but it has this
‫unique ID before it.
‫So even if someone else uploaded a file with this name, it would have a different unique ID before
‫it.
‫And you can see the uploads folder was also created.
‫Nothing in it, but it was created.
‫So now let's handle the actual upload.
‫So we'll get rid of that and let's say upload file.
‫And we can do this by first off creating an if statement.
‫And we're going to say if move uploaded file.
‫So this function does just that.
‫It'll move the file that you upload to wherever you want.
‫Now what it's going to take in as a first parameter is the temp name.
‫Remember there was that temp name that we have access to.
‫So we're going to pass in file and then temp underscore name.
‫All right then we want the location and the name of the file.
‫So I'm going to say upload dir.
‫And then just concatenate on the file name.
‫And the reason that I used a slash here is so that I could just do the directory and the file name if
‫I don't put the slash here.
‫And you can do this if you want, you can get rid of the slash.
‫But you'd have to add it down here and you'd have to concatenate that.
‫So it's up to you.
‫I just prefer to leave the slash in the name up here.
‫So if it uploads then let's just echo file.
‫Uploaded.
‫And then else.
‫If it doesn't, then we'll say file upload error.
‫And then I'm going to concatenate.
‫I'll put a colon and then concatenate onto that the file.
‫And then the error.
‫And that should do it.
‫So let's save that.
‫And then I'm going to refresh which is going to do the form submission.
‫Again we get file uploaded.
‫We take a look over here and there we go.
‫So there we have the thumbnail.
‫All right.
‫So that's is it easy as that to upload an image or any kind of file in PHP.
‫Now you might want to run some other checks.
‫For instance you might want to only accept images JPEGs and PNGs or whatever.
‫So let's do that.
‫I'm going to go under where we create the file name and let's say check file type.
‫So I'm going to have an array called allowed.
‫Extensions so that whatever we want to accept, we'll just go in this array.
‫So let's say we want.
‫JPG.
‫Jpeg and PNG.
‫So those are the only extensions that we want to allow.
‫Then we want to get the file extension of the file being uploaded.
‫So let's say file extension and let's set that to str to lower.
‫So it's just going to make it lowercase.
‫And then the way that we can get it the extension is with the path info function.
‫So if we do path info and then we pass in the file name and we can pass in here one of these dirname
‫base name extension and file name.
‫So we can get different things using this function.
‫What we want is the extension.
‫So we want that path info extension.
‫So that will give us whether it's PNG, Jpeg or whatever.
‫So now we just want to wrap the rest of this this upload mov upload file in an if statement.
‫I'll just put it up here first and we can say if n array.
‫And pass in the file extension.
‫And then we want to pass in the allowed extension.
‫So it's just going to check to see if it's in that array.
‫If so then we want to proceed to upload our files.
‫So we'll just cut that and we'll put that in here.
‫And we'll just put a comment here and say make sure make sure extension is in array.
‫And that should do it.
‫So now we'll actually let's put an error message.
‫So in the else of this if.
‫Let's just echo.
‫Invalid.
‫I guess invalid file type.
‫And semicolon.
‫Okay.
‫So let's try that out I'm going to go ahead and choose a file.
‫And I'm going to choose the MP for.
‫Which obviously is not in the array.
‫If I click create.
‫Um, all right, so this is actually giving me an error before it even hits my error.
‫It's giving me one that says it's too big because there is a limit in your PHP ini file of how big an
‫image can be.
‫So let's see, I guess what I could do.
‫Is can I?
‫I'm just going to create.
‫A new file real quick test dot txt.
‫All right.
‫And then I'm just going to put that on my desktop and I'm going to try to upload that.
‫So if I go to desktop test.txt which is not an image and I get invalid file type.
‫All right.
‫So that's how you can make sure that it's an image.
‫And there's other things you can check as well.
‫You can check the size.
‫Remember we have access to the size.
‫So you can limit it to however however big you want.
‫All right.
‫So in the next video we're going to do a little challenge.
‫And I want you guys to show this these messages inside of an alert.
‫So you can store the messages in an array.
‫And you can then output them nice and neat rather than just up here in the corner.