Check for Weak File Permissions

Last updated 1 month ago

PHP
function my_plugin_check_file_permissions() {
    $files = array(
        ABSPATH . 'wp-config.php',
        ABSPATH . 'wp-content/uploads/',
    );

    foreach ( $files as $file ) {
        if ( file_exists( $file ) ) {
            $permissions = substr( decoct( fileperms( $file ) ), -4 );
            if ( in_array( $permissions, array('0777', '0666') ) ) {
                echo '<div class="notice notice-error is-dismissible"><p><strong>' . $file . '</strong> has weak file permissions (permissions: ' . $permissions . ').</p></div>';
            }
        }
    }
}
add_action( 'admin_notices', 'my_plugin_check_file_permissions' );

All code snippets are licensed GPLv2 or later unless otherwise stated.