Welcome back. In the first part of this tutorial, I showed you how to take out specific properties of our uploaded file using this form here.
Now I'll show you how to upload this file and move it to the uploaded folder here which is currently empty.
If you recall, we're referring to a temporary area that is being stored on our web server.
Its not of much use right now.
We have all our properties here, so I'll say properties of the uploaded file so we know what we're doing.
We have all our specific properties here.
I've given all of them easy to remember variable names so we don't need to comment each of these individually.
First thing we will do is create an 'if' statement to check if there are any errors.
Here if error code is bigger than zero meaning it has been issued by an error code then I'll say 'die'
And I will give an error message out "File couldn't..."
Or 'Error uploading file, code error'.
This will give the user an error code.
Now the 'else' part.
I'll add these curly brackets to keep that simple and in a single line.
So 'else' I want to use a function called 'move_uploaded_file'.
Then we'll take the temporary name 'temp', which is the first parameter of this function and the second parameter is the destination which is 'uploaded folder'
So I'll type 'uploaded' and a forward slash.
And at the end of that we'll concatenate the name the file that we've uploaded.
So here it would just be 'name'.
This shows the user just adding the inter variables here.
Otherwise we would have to type these, for example - temp name.
Then go here and place it like this.
It gets quite messy and hard to read.
So its easier to just keep these variables here.
Okay so now I'll get rid of these or rather I'll keep these.
And lastly echo out a message saying 'Upload complete'.
Lets try this.
I logon to our page and pick our file - 'intro to avi'.
I'll click on upload and we can see that upload is complete.
Lets check my file.
Upload folder and click on my uploaded sub directory you can see that the file is here whereas before - it had been stored in the temporary directory on my web server.
So we've successfully uploaded our file here.
There are a few more things that we need to do.
Undo another 'if' statement or undo this 'if' statement.
We are going to check for specific file types that we don't want uploaded.
So for example lets say I don't want avi files to be uploaded.
What I could do here is say - if error is bigger than zero, don't upload files.
Otherwise I'll start a new 'if' statement inside the else
And I'll create a block here.
And these are the conditions for the file.
I'll say - if the type of file - that's our type variable, t-y-p-e, 2 equal to signs, equals video dot avi.
As you saw in the first part of this, as I echoed it out, it was equal to video dot avi.
And then we're saying that if it is equal to video dot avi then upload the file.
I'll just move it down here and I will put that into the 'else' block.
So now I have - if the video is equal to avi then die and the message is 'That format is not allowed'.
Okay so now I'll delete this from our uploaded directory and I'll come back to my initial uploaded file.
I'll choose intro dot avi and when I click upload it says that that 'format is not allowed'
And if you go to my uploaded directory you can see that the folder is empty.
Nothing has been uploaded.
Now instead of avi let us say I want to ban 'images with png' extension.
I'll change it here and upload my file again.
You can see that because its an accepted file format, we get the message 'Upload complete' and its been transferred to my uploaded folder.
Lets delete that again. Oh! I canceled it. Lets delete that again.
Okay. So what we've seen here is how to specify a specific type.
What we also can do is specify a specific file size.
I'll say 'or' using this 'or' operator and I'll say 'or' the size is bigger than half a megabyte.
This is half a megabyte, which is five hundred thousand bits sorry bytes. I think I made a mistake and said bits instead of bytes.
So that's five hundred thousand bytes which is equal to 0 point 4 megabytes. I'll just say half a megabyte for now.
This will evaluate the size and say is it bigger than half a megabyte.
Then it will say this format is not allowed.
So I'll change this message to accommodate 'Format not allowed or file size too big'.
So you can create an if statement for each of these that is for evaluating your type and evaluating your size.
You just need to take this condition and put it in another 'if' statement.
So I go back here and I'll choose my file again.
Just making sure its there.
Click upload and it'll say 'Format not allowed'.
Now if you go back to our code this is not in png format but it is exceeding the size limit.
Lets change this to 2 million which is 2 megabytes.
Refresh and send that.
We can see that our upload has been completed because this is only one megabyte in size.
That's all I have on File Upload for now.
This is all you need to know to use this to keep out specific file types and file sizes that are too large for your web server.
If you don't want big files on your web server this is a good way to control it.
Its very easy to create as you've seen.
Practice this and you'll be quite impressed with how useful this can be.
If you've got any questions please don't hesitate to ask.
Also please subscribe if you want to be notified on any updated videos or new videos.
Thanks for watching. This is Joshua Mathew dubbing for the Spoken Tutorial Project. Bye.