WEBVTT 00:00:06.660 --> 00:00:17.680 OK so now let's go ahead and start validating the user or the bodies to use inputs. 00:00:17.790 --> 00:00:27.290 So first we are going to check if the file exists. 00:00:27.420 --> 00:00:28.050 OK. 00:00:28.530 --> 00:00:32.110 They say check all areas. 00:00:32.110 --> 00:00:35.450 So first of all we're going to check if the file exists. 00:00:35.540 --> 00:00:36.810 OK so 00:00:40.550 --> 00:00:50.120 we're going to say if the condition we're going to we're going to assess it's using the file exist function 00:00:50.130 --> 00:00:52.150 . 00:00:54.500 --> 00:00:55.180 OK. 00:00:55.710 --> 00:00:57.030 So if you do control Kate 00:01:00.090 --> 00:01:04.770 you'll see that this function is going to check with a file or directory exists. 00:01:04.770 --> 00:01:05.320 OK. 00:01:05.550 --> 00:01:13.770 So we're going to give it's one argument which is going to be the permanent destination of our file 00:01:13.790 --> 00:01:14.500 . 00:01:15.170 --> 00:01:15.400 OK 00:01:15.470 --> 00:01:21.490 . 00:01:22.650 --> 00:01:34.350 And if the file exists or if this directory with the file in it exists then we are going to the error 00:01:34.350 --> 00:01:42.790 message to our error is variable as we haven't created yet but it's going to be created when we need 00:01:42.790 --> 00:01:43.600 this. 00:01:43.600 --> 00:01:50.940 We're going to add the error file already exists. 00:01:50.940 --> 00:01:52.310 All right. 00:01:53.290 --> 00:01:53.870 OK. 00:01:54.030 --> 00:02:00.790 So the second possible error is if the size is bigger than 3 megabytes. 00:02:00.960 --> 00:02:10.680 So if the size is bigger than 3 megabytes and the size is measured in bytes. 00:02:10.710 --> 00:02:20.340 So we need to say if the size is bigger than three multiplied by 1024 multiplied by one ton 24:2 gets 00:02:20.340 --> 00:02:22.670 the size in bytes. 00:02:22.970 --> 00:02:29.430 If the slot is too big then we are going to add to our areas variable 00:02:33.810 --> 00:02:35.580 file to large. 00:02:36.090 --> 00:02:37.900 OK. 00:02:38.160 --> 00:02:47.260 The other possible error is going to be the type of case you are wrong type. 00:02:47.280 --> 00:02:58.990 So how do we say that the up the uploaded file OK has got a type that is not supported. 00:02:58.990 --> 00:03:09.580 So its not PTF and its not text a good way to do that is by defining an array. 00:03:12.450 --> 00:03:17.700 Well we're going to place all the excepted types. 00:03:17.970 --> 00:03:28.470 So when I say allowed for months to upload and we're going to create a variable we're going to call 00:03:29.430 --> 00:03:31.860 aloud for months. 00:03:32.120 --> 00:03:35.880 So it's going to be an array. 00:03:35.970 --> 00:03:39.590 So are you going to go from a C array. 00:03:39.780 --> 00:03:43.280 So the first element is going to be pedia. 00:03:43.290 --> 00:03:47.610 So we're going to give it a key and a value. 00:03:47.640 --> 00:03:50.880 So the value is going to be the actual type. 00:03:51.000 --> 00:03:58.500 And for a PTF the mime type is going to be application for slush PTF and the key is just give it to 00:03:58.500 --> 00:03:59.430 believe. 00:03:59.960 --> 00:04:03.330 OK. 00:04:03.470 --> 00:04:09.120 And the second one for text is you're going to have the key and then the value 00:04:12.240 --> 00:04:19.060 and the value is going to be the my type which is text or slash play. 00:04:19.790 --> 00:04:20.420 OK. 00:04:20.850 --> 00:04:24.320 Then the key text. 00:04:24.390 --> 00:04:25.000 All right. 00:04:25.290 --> 00:04:33.930 So to check if the file is not supported all that type is not supposed to be you're going to say if 00:04:35.190 --> 00:04:37.460 the type is not inside the array. 00:04:37.560 --> 00:04:47.300 And if you remember the function in array will check whether a certain value is inside an array or not 00:04:47.310 --> 00:04:47.930 . 00:04:47.950 --> 00:04:50.890 OK so. 00:04:52.490 --> 00:04:57.230 So you want to check if the value is not in the arrays are going to use an exclamation mark. 00:04:57.510 --> 00:05:05.340 And we are going to give this function a couple of arguments so if you remember the first arguments 00:05:07.020 --> 00:05:08.010 was the 00:05:14.650 --> 00:05:20.220 the value to check and the second promiser was the array. 00:05:20.540 --> 00:05:21.770 OK so 00:05:21.780 --> 00:05:28.360 . 00:05:28.470 --> 00:05:37.530 So the first argument is the type and the second one is are allowed formats right. 00:05:38.860 --> 00:05:39.640 OK. 00:05:40.030 --> 00:05:46.300 So if O type is not supported then we are going to. 00:05:46.360 --> 00:05:48.280 Our air is variable 00:05:51.350 --> 00:05:55.800 the message wrong formats. 00:05:56.640 --> 00:06:00.190 OK. 00:06:00.730 --> 00:06:07.520 Now the other area left that is possible is if we haven't uploaded any file. 00:06:07.570 --> 00:06:10.170 OK let's put that at the very beginning. 00:06:10.900 --> 00:06:13.810 So if 00:06:18.460 --> 00:06:19.360 file error 00:06:22.570 --> 00:06:23.140 equals 00:06:26.470 --> 00:06:27.700 4. 00:06:28.460 --> 00:06:40.110 OK so this means that we haven't uploaded any file that we are going to add to the errors variable 00:06:42.730 --> 00:06:47.780 the message saying no files upload. 00:06:49.080 --> 00:06:49.290 OK 00:06:52.170 --> 00:06:57.390 OK so these are all the possible errors that we need to look for. 00:06:57.630 --> 00:07:05.710 OK so now we can go ahead and check if there are any errors. 00:07:05.910 --> 00:07:06.420 OK. 00:07:06.580 --> 00:07:11.710 Pre-NST the results message is going to be an error message. 00:07:11.710 --> 00:07:16.580 Otherwise we are going to upload our file to the permanent destination. 00:07:16.870 --> 00:07:17.550 OK. 00:07:18.010 --> 00:07:21.820 So if errors. 00:07:22.150 --> 00:07:28.290 Ok so now we've got one or more errors than our results message 00:07:31.880 --> 00:07:34.480 as we did in the context of session. 00:07:34.510 --> 00:07:37.450 So it's going to be a div 00:07:40.960 --> 00:07:41.880 of gloves. 00:07:41.910 --> 00:07:43.080 Alert alert danger 00:07:46.290 --> 00:07:46.840 class 00:07:49.600 --> 00:07:52.780 alerts alerts danger. 00:07:53.080 --> 00:07:53.590 OK. 00:07:53.800 --> 00:08:01.450 And inside the div we're going to our air is variable. 00:08:02.230 --> 00:08:13.110 All right and then we are going to echo the results message if we have no errors or else. 00:08:13.290 --> 00:08:18.150 And now we are ready to move our file to the permanent destination. 00:08:18.440 --> 00:08:19.280 OK. 00:08:19.360 --> 00:08:30.710 And we are going to do that's using a function called Move and the school uploaded the score file. 00:08:30.880 --> 00:08:31.440 OK. 00:08:31.630 --> 00:08:37.540 And you can check this function using control K and you can see that it's going to move and upload file 00:08:37.570 --> 00:08:46.810 to a new location so we know that at the moment our file is in a temporary location which is this one 00:08:46.830 --> 00:08:47.130 . 00:08:47.440 --> 00:08:54.160 OK but now we want to move it to our final destination which is the permanent destination which is this 00:08:54.160 --> 00:08:54.810 one. 00:08:55.330 --> 00:08:56.710 OK. 00:08:56.710 --> 00:09:03.970 So this function is going to take a couple of arguments so the first one is going to be the temporary 00:09:05.230 --> 00:09:06.080 location. 00:09:06.500 --> 00:09:08.370 OK. 00:09:08.590 --> 00:09:09.640 Which is this. 00:09:09.850 --> 00:09:15.130 And the second argument is going to be the permanent destination which is that's when we have Saudis 00:09:15.180 --> 00:09:18.250 inside our variable permanent destination. 00:09:18.880 --> 00:09:19.740 OK. 00:09:20.200 --> 00:09:25.740 So if we are right this motion like that so it's going to move the file if it's successful. 00:09:25.750 --> 00:09:29.890 So rather than writing it like this we just cannot check if it's successful. 00:09:29.890 --> 00:09:35.160 So we're going to say if they're inside if condition we're going to place our function. 00:09:35.710 --> 00:09:38.740 So if it's successful it's going to return true. 00:09:38.770 --> 00:09:40.300 OK. 00:09:40.360 --> 00:09:44.650 So if he says well we're going to set our results message 00:09:49.020 --> 00:09:49.940 to I. 00:09:49.990 --> 00:09:58.660 Dave of class alerts alerts success and we're going to say file upload it successfully 00:10:07.180 --> 00:10:09.110 OK. 00:10:10.650 --> 00:10:14.670 Otherwise we are going to 00:10:18.030 --> 00:10:33.570 say we're going to have a div of class wanting saying for example unable to upload file. 00:10:33.690 --> 00:10:37.450 Please try again later. 00:10:38.940 --> 00:10:41.210 All right. 00:10:41.820 --> 00:10:50.790 In both cases we need to echo the results message. 00:10:50.940 --> 00:10:51.360 All right 00:10:54.690 --> 00:10:57.020 now we are ready to test our code. 00:10:57.180 --> 00:11:01.800 But before we do that let's just have a quick look at the code and make sure that everything is fine 00:11:01.800 --> 00:11:02.160 . 00:11:02.190 --> 00:11:04.970 So we've got our final details there. 00:11:05.060 --> 00:11:05.470 OK. 00:11:05.460 --> 00:11:10.960 The name type size the error. 00:11:10.970 --> 00:11:12.970 OK so we call these file error. 00:11:13.290 --> 00:11:15.240 But the key is supposed to be error. 00:11:15.350 --> 00:11:18.230 OK let's make sure we correct this. 00:11:19.010 --> 00:11:25.640 OK and of course the name the parent in this nation they're OK and it's fine. 00:11:26.010 --> 00:11:28.830 And of course all the error messages to display. 00:11:29.110 --> 00:11:30.860 OK. 00:11:31.240 --> 00:11:37.620 And we called the allowed for months and finally we are checking for errors and we can make a quick 00:11:37.620 --> 00:11:40.780 change here for checking errors. 00:11:40.800 --> 00:11:47.080 It makes more sense to check the whether the file exists the size and the formats. 00:11:47.160 --> 00:11:49.350 Only if we have uploaded the file. 00:11:49.590 --> 00:11:50.180 OK. 00:11:50.190 --> 00:11:56.130 So are you going to do get that check if file error is 4 which means that we don't have any file. 00:11:56.160 --> 00:11:59.180 Then we can get this error message. 00:11:59.220 --> 00:12:01.260 No file to upload. 00:12:01.260 --> 00:12:04.390 Else we are going to check all this. 00:12:05.040 --> 00:12:16.750 So we're going to check these three or check or look for these three areas only if we have a file. 00:12:17.040 --> 00:12:21.180 OK. 00:12:21.970 --> 00:12:28.600 All right so now if we save let's try this. 00:12:28.740 --> 00:12:29.380 OK. 00:12:29.400 --> 00:12:33.900 So I'm going to try to upload a file. 00:12:35.590 --> 00:12:41.670 OK all right let's just look at some of my FCP and make sure that we don't have any files there in the 00:12:41.670 --> 00:12:43.550 uploads folder. 00:12:44.310 --> 00:12:44.620 OK. 00:12:44.670 --> 00:12:45.950 We have a deal on this one. 00:12:46.120 --> 00:12:50.810 So let's just delete this one now fresh. 00:12:50.820 --> 00:12:53.760 OK so now our uploads folder is empty. 00:12:53.880 --> 00:12:58.770 OK so let's upload a file. 00:13:01.020 --> 00:13:01.560 OK. 00:13:01.570 --> 00:13:05.040 You can see that's our file has been uploaded successfully. 00:13:05.040 --> 00:13:11.560 So let's go to our file FCP and make sure that we've got the file in the file. 00:13:11.620 --> 00:13:12.760 Reports are filed. 00:13:12.900 --> 00:13:18.250 OK so let's try for example to upload the same file again 00:13:21.610 --> 00:13:22.370 we can see this. 00:13:22.380 --> 00:13:26.490 If you don't use any file It's curious the write error message. 00:13:26.520 --> 00:13:28.260 If we try to upload the same file 00:13:32.500 --> 00:13:35.190 I can see it saying this file already exists. 00:13:35.200 --> 00:13:37.680 OK OK. 00:13:37.720 --> 00:13:40.940 And it's check the size error. 00:13:41.250 --> 00:13:44.240 So it's change the size for example to 00:13:47.870 --> 00:13:51.210 1. 00:13:52.770 --> 00:13:56.690 You look at it 00:14:00.100 --> 00:14:04.240 which is 1024 bytes or oxes. 00:14:04.410 --> 00:14:08.670 So now let's try to upload a 00:14:11.670 --> 00:14:17.830 file bigger than this and we use our PDA file 00:14:23.670 --> 00:14:25.640 and try to upload it. 00:14:26.380 --> 00:14:30.100 You'll see that we see our size right. 00:14:30.200 --> 00:14:30.650 OK. 00:14:30.660 --> 00:14:34.610 You can only upload files smaller than 3 megabytes. 00:14:34.660 --> 00:14:35.970 OK we need to change Dadds. 00:14:36.010 --> 00:14:37.580 OK. 00:14:37.800 --> 00:14:47.490 Two smaller than one killer octet but we just try to make sure that the sys error is working. 00:14:47.640 --> 00:14:48.380 OK. 00:14:48.820 --> 00:14:58.650 So let's move back to the original size and now if you try to upload the file 00:15:01.980 --> 00:15:03.400 it's so let's choose it again 00:15:06.140 --> 00:15:09.740 it and I can see that the file has been uploaded successfully. 00:15:09.850 --> 00:15:15.960 If we go to the file FTB we can check that information and we can see our pedia file. 00:15:16.230 --> 00:15:18.300 OK. 00:15:18.610 --> 00:15:19.010 OK. 00:15:19.140 --> 00:15:25.070 So that's how we can upload files using BHB. 00:15:25.870 --> 00:15:32.130 OK so if you would like to redirect the user to I think your page once they have uploaded their file 00:15:32.130 --> 00:15:32.430 . 00:15:32.590 --> 00:15:38.700 You can do that so rather than printing the success message you can use the habere function as we have 00:15:38.700 --> 00:15:40.300 done with the context font. 00:15:40.500 --> 00:15:41.180 OK. 00:15:41.400 --> 00:15:43.780 So let's go ahead and do this. 00:15:43.840 --> 00:15:47.490 So so if it's successful. 00:15:47.590 --> 00:15:53.640 So rather than doing this OK session the results message on it and then you're going to use the header 00:15:53.700 --> 00:15:58.140 function to direct the user to the thank you. 00:15:58.140 --> 00:15:59.550 Page OK. 00:16:00.240 --> 00:16:02.980 So we're going to use location. 00:16:03.010 --> 00:16:07.880 CONAN And we are going to the file 26 dots. 00:16:08.460 --> 00:16:11.140 Thanks for your file. 00:16:11.150 --> 00:16:12.940 Those BHB OK. 00:16:13.320 --> 00:16:14.750 Save me save. 00:16:14.750 --> 00:16:16.090 So let's try this. 00:16:16.100 --> 00:16:20.000 So first let's empty our uploads folder. 00:16:20.330 --> 00:16:21.140 OK. 00:16:21.390 --> 00:16:31.860 Just delete these files and let's upload a new file upload this one for example and I can see that we 00:16:31.870 --> 00:16:34.970 have been directed to the thank you page. 00:16:35.010 --> 00:16:40.350 So they just said populates our page so we can just copy the content from the Contacts form. 00:16:40.440 --> 00:16:41.780 Thank you page. 00:16:42.460 --> 00:16:43.520 OK. 00:16:43.920 --> 00:16:45.970 I just change the title too. 00:16:46.170 --> 00:16:49.480 Thanks for your file and change the text here too. 00:16:49.530 --> 00:16:50.700 Thanks for your file. 00:16:50.860 --> 00:16:51.880 OK. 00:16:52.980 --> 00:16:55.890 So now if we go back and 00:16:58.650 --> 00:16:59.510 test this. 00:16:59.670 --> 00:17:01.580 OK. 00:17:02.590 --> 00:17:08.590 So let's upload another file. 00:17:10.200 --> 00:17:10.450 OK. 00:17:10.470 --> 00:17:15.480 Now we can see that we have been directed to the thank you page. 00:17:15.570 --> 00:17:20.300 OK.