How can PHP beginners distinguish between different file types when uploading files?

PHP beginners can distinguish between different file types when uploading files by checking the file extension or MIME type. This can be done using the pathinfo() function to extract the file extension or the $_FILES['file']['type'] variable to get the MIME type. By validating the file type before allowing the upload, beginners can prevent potentially harmful files from being uploaded to their server.

// Check file extension
$allowedExtensions = array('jpg', 'jpeg', 'png', 'gif');
$extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);

if (!in_array($extension, $allowedExtensions)) {
    echo "Invalid file type. Please upload a JPG, JPEG, PNG, or GIF file.";
}

// Check MIME type
$allowedMimeTypes = array('image/jpeg', 'image/png', 'image/gif');
$mime = $_FILES['file']['type'];

if (!in_array($mime, $allowedMimeTypes)) {
    echo "Invalid file type. Please upload a JPG, JPEG, PNG, or GIF file.";
}