Contact Us

6ta. Avenida 2-48 zona 13, Guatemala, Guatemala.

T +(502) 2458-4464 • Email: info@pixtig.com

Write us :)

Quote
Your budget in US Dollars ($)
Current Website (if you have one)
I want to be part of your team
Inquiry
Can you help us?

Blog

We are passionate about Drupal and obsessed with adding significant content to the web. We design, we program, we develop, we comunicate, we teach and blogging.

rosario's picture

Upload profile picture Drupal 7

By: 
rosario
Tags: 
Drupal 7
Images
Sometimes we need to customize the user profile form and it is therefore necessary to raise or edit profile picture, for it can use the following script:
Código: 
<?php
 
function MODULE_pics_form($form, &$form_state) {
 
  // Attach User Picture upload
  $form['picture']['profile'] = array(
    '#type' => 'file',
    '#title' => t('Profile picture')
  );
 
  $form['submit'] = array('#type' => 'submit','#value' => 'Submit');
 
  return $form;
}
 
 
function MODULE_pics_form_validate($form, &$form_state) {
  $file = file_save_upload('profile', array(
      'file_validate_is_image' => array(),
      'file_validate_extensions' => array('png gif jpg jpeg'),
  ));
  if ($file) {
    if ($file = file_move($file, 'public://')) {
      $form_state['values']['profile'] = $file;
    }
    else {
      form_set_error('profile', t('Failed to write the uploaded file the site\'s file folder.'));
    }
  }
  else {
    form_set_error('profile', t('No file was uploaded.'));
  }
}
 
function MODULE_pics_form_submit($form, &$form_state) {
  global $user;
  $uid = $user->uid;
  $account = user_load($uid);
 
  $file = $form_state['values']['profile'];
  unset($form_state['values']['profile']);
  $file->status = FILE_STATUS_PERMANENT;
  file_save($file);
 
  $edit['picture'] = $file;
  user_save($account, $edit);
 
}
 
?>