PHP-and-MySQL/C4/File-Upload-Part-2/English

From Script | Spoken-Tutorial
Jump to: navigation, search
Time Narration
0:00 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.
0:10 Now I'll show you how to upload this file and move it to the uploaded folder here which is currently empty.
0:18 If you recall, we're referring to a temporary area that is being stored on our web server.
0:25 Its not of much use right now.
0:29 We have all our properties here, so I'll say properties of the uploaded file so we know what we're doing.
0:34 We have all our specific properties here.
0:38 I've given all of them easy to remember variable names so we don't need to comment each of these individually.
0:46 First thing we will do is create an 'if' statement to check if there are any errors.
0:53 Here if error code is bigger than zero meaning it has been issued by an error code then I'll say 'die'
1:03 And I will give an error message out "File couldn't..."
1:11 Or 'Error uploading file, code error'.
1:20 This will give the user an error code.
1:23 Now the 'else' part.
1:25 I'll add these curly brackets to keep that simple and in a single line.
1:29 So 'else' I want to use a function called 'move_uploaded_file'.
1:39 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'
1:51 So I'll type 'uploaded' and a forward slash.
1:59 And at the end of that we'll concatenate the name the file that we've uploaded.
2:07 So here it would just be 'name'.
2:10 This shows the user just adding the inter variables here.
2:15 Otherwise we would have to type these, for example - temp name.
2:19 Then go here and place it like this.
2:22 It gets quite messy and hard to read.
2:25 So its easier to just keep these variables here.
2:33 Okay so now I'll get rid of these or rather I'll keep these.
2:37 And lastly echo out a message saying 'Upload complete'.
2:41 Lets try this.
2:47 I logon to our page and pick our file - 'intro to avi'.
2:51 I'll click on upload and we can see that upload is complete.
2:55 Lets check my file.
2:57 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.
3:08 So we've successfully uploaded our file here.
3:13 There are a few more things that we need to do.
3:15 Undo another 'if' statement or undo this 'if' statement.
3:20 We are going to check for specific file types that we don't want uploaded.
3:24 So for example lets say I don't want avi files to be uploaded.
3:30 What I could do here is say - if error is bigger than zero, don't upload files.
3:37 Otherwise I'll start a new 'if' statement inside the else
3:41 And I'll create a block here.
3:47 And these are the conditions for the file.
3:51 I'll say - if the type of file - that's our type variable, t-y-p-e, 2 equal to signs, equals video slash avi.
4:09 As you saw in the first part of this, as I echoed it out, it was equal to video slash avi.
4:19 And then we're saying that if it is equal to video slash avi then upload the file.
4:28 I'll just move it down here and I will put that into the 'else' block.
4:32 So now I have - if the video is equal to avi then die and the message is 'That format is not allowed'.
4:44 Okay so now I'll delete this from our uploaded directory and I'll come back to my initial uploaded file.
4:54 I'll choose intro dot avi and when I click upload it says that that 'format is not allowed'
5:01 And if you go to my uploaded directory you can see that the folder is empty.
5:06 Nothing has been uploaded.
5:08 Now instead of avi let us say I want to ban 'images with png' extension.
5:15 I'll change it here and upload my file again.
5:23 You can see that because its an accepted file format, we get the message 'Upload complete' and its been transferred to my uploaded folder.
5:33 Lets delete that again. Oh! I canceled it. Lets delete that again.
5:42 Okay. So what we've seen here is how to specify a specific type.
5:47 What we also can do is specify a specific file size.
5:51 I'll say 'or' using this 'or' operator and I'll say 'or' the size is bigger than half a megabyte.
6:04 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.
6:14 So that's five hundred thousand bytes which is equal to 0 point 4 megabytes. I'll just say half a megabyte for now.
6:29 This will evaluate the size and say is it bigger than half a megabyte.
6:38 Then it will say this format is not allowed.
6:43 So I'll change this message to accommodate 'Format not allowed or file size too big'.
6:56 So you can create an if statement for each of these that is for evaluating your type and evaluating your size.
7:03 You just need to take this condition and put it in another 'if' statement.
7:09 So I go back here and I'll choose my file again.
7:12 Just making sure its there.
7:14 Click upload and it'll say 'Format not allowed'.
7:19 Now if you go back to our code this is not in png format but it is exceeding the size limit.
7:25 Lets change this to 2 million which is 2 megabytes.
7:31 Refresh and send that.
7:33 We can see that our upload has been completed because this is only one megabyte in size.
7:39 That's all I have on File Upload for now.
7:44 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.
7:54 If you don't want big files on your web server this is a good way to control it.
7:58 Its very easy to create as you've seen.
8:01 Practice this and you'll be quite impressed with how useful this can be.
8:05 If you've got any questions please don't hesitate to ask.
8:08 Also please subscribe if you want to be notified on any updated videos or new videos.
8:15 Thanks for watching. This is Joshua Mathew dubbing for the Spoken Tutorial Project. Bye.

Contributors and Content Editors

Chandrika, Gaurav, Pravin1389