How can you restrict certain file types from being uploaded in PHP?

To restrict certain file types from being uploaded in PHP, you can check the file type before allowing the upload to proceed. You can achieve this by checking the file's MIME type or extension against a list of allowed file types. If the file type is not allowed, you can reject the upload.

$allowedFileTypes = ['image/jpeg', 'image/png', 'application/pdf']; // Define the allowed file types

if (in_array($_FILES['file']['type'], $allowedFileTypes)) {
    // Process the file upload
} else {
    echo 'Invalid file type. Only JPEG, PNG, and PDF files are allowed.';
}