Processing the File Upload
See how the uploaded file is processed.
Creating a directory for uploads
When PHP sees that a file is being uploaded, it will save that file to a temporary location.
Once we have validated the other form fields, we should move that file to a publicly accessible directory.
We want to show this image on our website, so it should be somewhere within the document root of the server, that is, in public/
.
It would be smart to make a distinction between the static resources of the website itself (elephpant.jpg
, bootstrap.min.css
, etc.) and dynamic resources that are uploaded by users.
So let’s create a dedicated directory for uploaded files in public/
.
Let’s call this directory uploads
.
Validating the form data before saving the uploaded file
We shouldn’t move an uploaded file to public/uploads/
before we have validated data in the rest of the form.
If the user has provided invalid input (for instance, the destination is missing), we shouldn’t process the file upload yet ...