PHP Tutorial For Beginners: File Upload [w/ subs]

00:00:00 – in this video tutorial you will learn how to create PHP file upload script so
00:00:05 – stay with me
00:00:15 – Hey, what's up guys Senaid here from
00:00:19 – the place where I help others to become a web
00:00:22 – developer much easier and faster and then they will do it on they own so if that
00:00:27 – is something that interests you consider subscribing so now I'm here at my phpstorm
00:00:34 – and I have created an empty PHP file upload project and also I have created a
00:00:41 – new empty uploads folder and an empty index.PHP file so first thing that
00:00:50 – we need to do is we need to check are we able to upload files to this server so
00:00:57 – in my example I will use my localhost which is
00:01:01 – XAMPP server and I know that it's already alowed for me to upload but for some
00:01:08 – of you it may be it's not so you will check it by opening PHP tags and then
00:01:17 – here we will say echo and call the function PHP info and let's open this
00:01:25 – in the browser so as you can see I have got some informations about my server
00:01:33 – as you can see using PHP version 7 and here you can see loaded
00:01:45 – location and open this file you can go manually but I will use my terminal and
00:01:51 – just like get it and hit enter and this file is opened so now I will search for
00:01:59 – file uploads and as you can see at the moment for me it is set to on it could be
00:02:07 – set to off or maybe can be like this so whatever the case
00:02:20 – that I save the file and you are set for this tutorial also down below there are
00:02:27 – some other settings left like maximum file size that you can upload also
00:02:42 – now first I'm going to create an empty html document so I will just say
00:02:51 – html in the head I will just have title and I will say PHP file upload and
00:03:01 – here I will say body and inside body our – case are simple html form so
00:03:11 – I will say form method I will use post action
00:03:18 – I will this same index.PHP file and since we are uploading file we need to set
00:03:25 – encryption part to the multi-part form data okay and now we just need two inputs one
00:03:34 – input the type will be file I will give it some name let's say
00:03:41 – attachment and we need one button which is of course submit button so that is
00:04:02 – choose file we can click there use any file it's open and then click on the
00:04:12 – upload and as you can see right now it is nothing happening because we do not have
00:04:19 – any PHP code that will handle the post from this form so in the
00:04:25 – PHP let us first let's say this and here I will say
00:04:36 – if(isset($_post['submit']); which means if someone press this button
00:04:45 – will execute the part of the code that files so I can say
00:05:02 – target file=uploads so the folder will be uploads and now in
00:05:09 – this folder we need to set the file name how we will store it so what I'm going
00:05:24 – array so let me just show you what it is so let's say I will just print this
00:05:37 – okay so continue choose file okay I would upload and as you can see now this
00:06:03 – files is an array associative array and inside of this array I have another
00:06:12 – which is this type of the file we have temporary name we have error which means
00:06:35 – to get this name I will say files attachment name we can also test that
00:06:44 – let's say like this files attachment in that name and let's
00:06:56 – see that as you can see we have that so right here is I want that my target file
00:07:13 – is in the folded upwards and then the name will be whatever the name of the
00:07:20 – file is but he are using the base name want so we just want to get the file
00:07:33 – name and that's what this base name function is good so if you want we can
00:07:41 – echo this target file just to make sure it's looking good as you can see our
00:07:49 – target files upload and to this folder uploads folder we will need a function
00:08:03 – move uploaded file so i will use files
00:08:23 – this location file to this location so we'll use this temporary name okay
00:08:38 – and you can say echo file uploaded so as you can see I'm using here if data
00:08:56 – if the file is uploaded or not and if it is we will execute part of the
00:09:05 – code let's say in this example will execute echo file uploaded continue it says file
00:09:21 – got my picture.png file so what will happened if I try again it's a work
00:09:32 – again but the question is how can we take it the same file already exists and
00:09:40 – using the function if file exists so I will say if file- target file I can see echo
00:10:00 – echo file already with this name already exists now as they see else if
00:10:22 – okay so let's test now continue it says filed with this name already exist
00:10:30 – which means we are not going to upload this file again until we have different
00:10:35 – file so we can go to the for example that this is open hit upload and it says
00:10:43 – file uploaded and now I have another file the thing is now how can we protect
00:10:52 – type of the file so let's say you want to create an image uploading form and you
00:11:13 – want to get the extension of the file so I can say extension = pathinfo() and I will
00:11:21 – use pathinfo() function and here I will just the target file and let's say I
00:11:34 – will check for PHP so let's see what is extension echo
00:11:57 – extension it says so the extension is this here sql so we can
00:12:11 – say here if extension different than and experience different them
00:12:28 – and let's say different then jp jpeg echo
00:12:50 – only the image files are allowed now let's say he else if okay so let's say let's
00:13:02 – test it now I'll try to upload again and see only the image files are allowed
00:13:12 – because the extension of our file is not any of the image file that we define it
00:13:18 – so if we try now to upload the picture and I hit upload it says file uploaded
00:13:26 – because the extension is png and the last thing that we can check is we
00:13:37 – can find the size of the file that is uploading and we can limit how big file
00:13:45 – it is possible to upload so as you can see from this array we have size and as
00:13:53 – you can see here this is 312 kilobytes because this is in the bytes
00:14:00 – so what you can do is let's say we will limit so that all the files above 200
00:14:06 – kilobytes are not possible to upload so we can say if files
00:14:15 – attachment size bigger than 200 kilobytes echo “your file
00:14:36 – is too big” else okay and let's that's now so if I try now to upload the same
00:14:53 – file again I can go to the upload and says your file is too big okay so let's
00:15:02 – just for the purpose of this tutorial I will set wing to the 500 kilobytes so
00:15:08 – that we can upload and let's test again now this file with this name already
00:15:16 – exists which means that we passed this this and the problem of code right here
00:15:23 – so what I can do is I will just go and delete the file and test again and say
00:15:35 – it's now file uploaded and here we go so we don't need this other top anymore
00:15:47 – it was does the purpose to show you how to do it so guys pretty much that's it
00:15:55 – this is a really basic and I hope that it's simple for you to understand this if you
00:16:01 – have any questions please ask in the comments below and I will be happy to
00:16:05 – answer and also if you liked this tutorial please like it and share with your
00:16:10 – friends Take care

Video Url: