When a file is uploaded the server saves the file to a temporary location on the server which is returned
in the temp name elements of the array.
This folder will be different depending on your operating system but which ever folder it is.
It's not accessible through a browser.
Additionally the operating systems temporary folders tend to be automatically emptied on a regular basis
are when the server is rebooted.
So once we have a file uploaded to the server we need to move it from this temporary folder and file
name to a more permanent location.
First let's create a folder in the web Root called uploads
where we're going to store any upload it files.
Then to move the uploaded file to this folder.
We can use the move uploaded file function which exists specifically for this purpose.
When we upload a file in addition to the path of the temporary file we also have the name of the original
file in the name elements of the array.
Let's create a variable with the path to the File we're going to move the uploaded file to.
This is the uploads folder we just created which is one level up so we need the two dots followed by
the name of the original file from the files array.
Then we can call the move uploaded file function passing in the path of the temporary file and the destination
path.
This function returns a boolean result.
So let's check that and print out a message.
Now if it returns FALSE we'll throw an exception.
Let's try uploading a valid image file.
We get an error.
Depending on the configuration of your computer this might have worked for you with no further changes.
If you are using Windows for example this probably worked.
If so then you can safely move on to the next video.
The area we're getting here is to deal with permissions on the uploads folder.
If you using Mac or Linux you might have got an error message like this.
This is because the web server user doesn't have a right permission to the upload folder
to fix this.
First let's find out which user the web server is using.
We can do that in the Apache configuration file.
And here we can see that the web server runs as the demon user.
In Linux you can change the group owner of the upload folder using this command.
And then the web server user will have permission to write a file to that folder.
On OS X on a Mac.
Something similar might work are.
You might need to grant read and write permissions on the parent folder of wherever you have your web
route.
It all depends on how your computer is configured and which web server you are using and so on.
But if you do need help please don't hesitate to ask.
Once the permissions are fixed if we got to load a valid file
we get the message that the file was uploaded successfully.
And there's the file in the uploads folder.