rosario.duenas
Enviado por rosario_dl el Lun, 13/04/2015 - 15:29

Algunas veces necesitamos personalizar el formulario de perfil de usuario y para ello es necesario subir o editar la foto de perfil, para eso pueden utilizar el siguiente script:

<?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);
 
}
 
?>