Search⌘ K
AI Features

Form Validation for File Uploads

Understand how to enforce file upload requirements in PHP forms by validating if users have submitted a picture when creating or editing entries. Learn to check the uploaded file's MIME type using finfo to confirm it is a JPG image and display appropriate error messages. This lesson helps you secure and improve user input handling for file uploads in PHP applications.

Checking if the user has uploaded a picture

So far, providing a picture was optional. What if we want to force the user to upload a picture? We will have to cover two scenarios:

  1. When the user is creating a new tour, we should verify that the user has uploaded a picture.
  2. When the user is editing a tour, we should verify that the user has uploaded a picture before or that they are uploading a picture now.

Instead of implementing a similar check in two different files, let’s make this part of the validation function we already have in tour-crud.php ...