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