What are some common security measures to prevent websites from being hacked and having unauthorized files uploaded?

Common security measures to prevent websites from being hacked and having unauthorized files uploaded include implementing file upload restrictions, validating file types, using secure coding practices, regularly updating software and plugins, and implementing strong password policies.

// Example code snippet for file upload restrictions
if(isset($_FILES['file'])) {
    $allowed_extensions = array('jpg', 'jpeg', 'png', 'gif');
    $file_extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
    
    if(!in_array($file_extension, $allowed_extensions)) {
        die('Invalid file type. Only JPG, JPEG, PNG, and GIF files are allowed.');
    }
    
    // Process file upload
}