title: Drupal 7 set user picture (avatar) programmatically
body: <p>I have form with file upload element. This file (image) must be user picture, but it not set.</p>
  
  <p>form:</p>
  
  <pre><code>      $form['f']['step4']['file_e'] = array(
      '#title' =&gt; t('Image'),
      '#type' =&gt; 'managed_file',
      '#description' =&gt; t('The uploaded image will be displayed on this page using the image style choosen below.'),
      '#upload_location' =&gt; 'public://images/',
    );
  </code></pre>
  
  <p>validate</p>
  
  <pre><code>function fill_up_profile_form_validate($form, &amp;$form_state) {
    global $user;
  
    if (isset($form['f']['actions']['submit']['#value']) &amp;&amp; $form_state['triggering_element']['#value'] == $form['f']['actions']['submit']['#value']) {
      $file = file_load($form_state['values']['f']['step4']['file_e']);
      $validators = array(
        'file_validate_is_image' =&gt; array(), 
        'file_validate_size' =&gt; array(1 * 1024 * 1024),
      );
  
      $errors = file_validate($file, $validators);
      if (!empty($errors)) {
        $message = '';
        foreach ($errors as $error)
          $message .= $error . '&lt;br/&gt;';
  
        form_set_error('Error!', $message);
      } else {
        $file-&gt;status = FILE_STATUS_PERMANENT;
        file_save($file);
        $edit['picture'] = $file;
        user_save($user, $edit);
      }
    }
  }
  </code></pre>
  
  <p>Thank you for your help.</p>