How can PHP be used to validate and handle different file types during the upload process?

When uploading files using PHP, it is important to validate the file type to ensure security and prevent malicious uploads. One way to do this is by checking the file's MIME type before allowing the upload to proceed. This can be done using the `$_FILES` superglobal array in PHP to access the file information.

// Check if file type is allowed
$allowedTypes = ['image/jpeg', 'image/png', 'image/gif'];
if (!in_array($_FILES['file']['type'], $allowedTypes)) {
    echo 'Invalid file type. Allowed types are: JPEG, PNG, GIF';
    exit;
}

// Handle file upload
$targetDir = 'uploads/';
$targetFile = $targetDir . basename($_FILES['file']['name']);

if (move_uploaded_file($_FILES['file']['tmp_name'], $targetFile)) {
    echo 'File uploaded successfully';
} else {
    echo 'Error uploading file';
}