Enabling users to upload files

I have added this code to enable everyone to add files

function allow_own_attachments( $user_caps, $req_caps, $args, $UserObj ) {
if ( empty($args[2]) ) {
   return $user_caps;  // nothing to check
}
$post = get_post( $args[2] );  // post_id was passed here

if (is_object($post)){  //check if $post is an object. If it is't checked the code throws this Notice: Trying to get property 'post_author' of non-object 
    if ( $post->post_author == $UserObj->ID ) {  // this is my post
        foreach ( (array) $req_caps as $cap ) {
            if ( empty( $user_caps[ $cap ] ) )
                $user_caps[ $cap ] = true;
        }
    }
}
$user_caps['edit_post'] = true; // tested by wp_ajax_upload_attachment()
return $user_caps;
}

add_filter( 'user_has_cap', 'allow_own_attachments', 10, 4 );

“…any custom filter that is determined to give permission must loop through the array and set everything true.”

I’ve used the code from another question. Original answer can be found here:
Add Media Upload Capabilities Needed for Custom Role for non-Posts