Hi. In this tutorial I'll show you how to create a simple php upload script.
This will be slightly more advanced in our upload dot php file.
We will use our index dot php. We will be mainly using html code to give a form to the user to submit this particular file.
Then in upload dot php we will process this file, get some information about the file like its name, its type, size, temporary stored name and any error messages that have occurred.
You can use the error messages to check if it has occurred or hasn't occurred.
Then we are going to process the file and save it in a specific directory on our web server.
In the second part of this tutorial, I'll quickly teach you how to check the specific file type so you can protect it against file types.
We will also learn how to check the file size of the file so you can have a maximum or minimum file size.
So here I've created a folder named 'uploaded' in which I've created my index and upload dot php files.
And this is where my files are being stored once they have been uploaded.
When the files are uploaded initially they go into a temporary area on the web server and NOT into this folder.
For the html - we need to create a form. To do this we have a form action and we have a specific action which is 'upload dot php' and we've created our file here.
The method is set to POST. The reason for this is that we don't need to store it in a GET variable.
Why? Because we don't want the user to see the binary data we're sending across the website, for security reasons.
And also there is a hundred character limit on our GET variable.
So youâ€™ll have a very small file if you have only hundred bits of data.
Okay, we have another parameter which you may not have heard of before.
It's enctype, encoding type which means how we are going to encode this.
It will be multi-part and we need a forward slash and then its form data.
In short this means that we're submitting this form in the form of data - that is, binary data - zeroes and ones which I mentioned earlier over here.
Okay so we've got the type this is going to be encoded to. We'll end our form here.
We'll need some elements inside as input for our file.
This type is set to file and we'll call it myfile to be specific.
Okay - paragraph break here and then all we need is a submit button,
Okay so lets just preview this. Lets close this off.
Click file upload. Oh - lets go back. Input - I typed 2 'u's here.
Lets go back. You can see here we got our input here.
I can make it browse. We can see we got a selection of files which we can upload.
Okay - so lets make it more user friendly for you and me.
Upload a file. And lets refresh that. Okay so we've got a nice page here.
We've got our header and possibility to upload a file here. Also can be manually typed in here if needed.
And also we have an upload button which submits to our upload dot php.
Fine. So inside upload dot php we need a way of processing this file which has been submitted from our form.
The way to do this is by using dollar underscore FILES. This isn't actually right.
We can tell it isn't right by echoing out just a single instance of this.
When we do and I click on upload, we can see we just get Array. That's because this is an array.
Since this is a multidimensional array, in the first set of brackets we'll type the name of the file that we have uploaded and the name of the input box from which it came from - that is myfile.
So we'll use myfile here. And in the second one we can have a variety of properties and the simplest and most easy one to think about is the name of the file.
So lets go back to the upload form and lets choose intro dot avi. It will be displayed here.
Lets click upload and on the next page we see intro dot avi.
Remember this is in our upload dot php form, file sorry.
Okay so that's that. Let me just save this to a variable.
The next one we'll look at is - I'll just type it here - is the type of file.
So this is dollar underscore files and we'll use the myname reference.
And inside here we'll have the type. So that's type or rather we'll echo this out so you can see.
And it's refresh. Re-send that and see that now - myfile.
Right re-send that and we can see there's a video slash avi. You may have seen this somewhere before in html.
For example - this can be image slash png or image slash jpeg, image slash bmp , video slash avi and video slash mpeg or some other format.
At the moment we can see from here that it is an avi file so therefore that's what we get in 'type'.
So we can say type equals all of this.
The next one I'll show you is the size. So to save time what I'll do is - I'll copy this code,paste it here and change this type to size and echo it out.
You can see it's quite simple to get e-property of the file you submit.
I'll refresh this page and click resend and we can get the size of the file.
Now lets say we round this off to about a million - a million bytes is in fact a....,
Sorry, a million bits is a megabyte. myfile is actually a mega byte.
So we've got a million megabyte of data here.
So lets save that in a variable called size. Ok?
Right then, the next one which is quite an important one is the 'temporary name'.
This is written slightly differently as tmp abbreviated to temp and underscore and name.
This will give us the directory that it's stored in temporarily until we transfer it to the folder of our choice.
So lets refresh this page.
Click on Resend and you can see that that's stored into an xampp cause I'm using this application
But if you're using an apache you can store php yourself.
You'll have apache here followed by your temporary file name.
You can see that this is a randomly generated name that has a tmp extension.
But it is useless to us at the moment.
So we can save that as temp file or temp. Lets type 'temp' in order to keep it short.
And the last one is 'error'. Now this will basically echo out a zero if everything is clear.
Again copy-paste and change that to error.
We should get zero at the moment because everything is written correctly.
And this will never be a negative value.
If it's more than zero it means that it's giving an error code which basically means that you have an error.
And let's say we'll store this in a variable called error.
Okay that's all for now. In the second part of this tutorial I'll show you how to upload your file by moving it from the temporary storage area to a specified area of your choice.
And what we'll do is we'll use this variable error to see if there are any errors.
If there are errors, then we'll echo it out and use the error code.
If not we'll take this temp and we'll use a specific function called 'move uploaded' file and we'll take that and store it in our uploaded directory created on my web server here.
And then after that I'll take you through some specifics to say - is this a jpeg? Yes then disallow jpeg image being uploaded or disallow specific file size.
Okay so tune in to Part-2. Thanks for watching. This is Joshua Mathew dubbing for the Spoken Tutorial Project.