Uploading a File
Explore how to add file upload functionality to a PHP form, understand multipart/form-data encoding, and manage uploaded files with the $_FILES superglobal. Learn to validate uploads, check for errors, and securely process file data within a PHP application without relying on frameworks.
Adding a new input element to the tour form
The first step will be to add a new <input> element to the tour form in pages/snippets/_tour_form.php:
The type attribute of this new element is set to "file".
For file inputs, the browser automatically renders a Browse... button, and it also shows which file you have selected.
You also have to change something about the <form> element itself.
To be able to upload files, the enctype attribute should be set to multipart/form-data:
Uploading a picture
Find a nice picture of Berlin (or your own favorite destination) on the internet.
Make sure to open the Network panel before you continue.
Go to http://APPLINK/create-tour, click Browse..., select the picture you just downloaded, and submit the form.
Select the POST request for create-tour and open the Request tab.
This shows the result of using the multipart/form-data encoding:
As ...