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