Upload picture in custom register form I'm building a multistep register form in a custom module. I'm using profile2 module to save other custom fields. I want to let the users choose an image from their computers. This is the code that I use to upload the picture that saves in the file_managed Drupal table but without the user uid because at this point I don't create the user.

$form['empresa']['logo_empresa'] = array(
        '#title' =&gt; t('Logo de la empresa.'),
        '#type' =&gt; 'managed_file',
        '#description' =&gt; t('Tamaño máximo 3Mb y formato aceptado jpg,jpeg o png'),
        '#upload_location' =&gt; 'public://',
        '#upload_validators' =&gt; array(
        'file_validate_extensions' =&gt; array('png jpg jpeg'),
        'file_validate_size' =&gt; array(3*300*300),
        ),
);


When all fields are OK I submit the form and create the user as follows:

function empresas_registration_signup_form_submit($form, &amp;$form_state) {


$nombre = $form_state['storage']['field_nombre_contacto'];
$apellidos = $form_state['storage']['field_apellidos_contacto'];
$mail = $form_state['storage']['email'];
$comunidad_autonoma = $form_state['storage']['field_comunidad_autonoma'];

$provincias = $form_state['storage']['field_provincia'];
$razon_social = $form_state['storage']['field_razon_social'];
$cif_nif = $form_state['storage']['field_cif_dni'];
$telefono = $form_state['storage']['field_telefono'];
$web = $form_state['storage']['field_web'];

$municipios = $form_state['storage']['field_municipio'];
$poblacion = $form_state['storage']['field_poblacion'];
$direccion = $form_state['storage']['field_direccion'];
$codigo_postal = $form_state['storage']['field_codigo_postal'];

$razon_social = $form_state['storage']['field_razon_social'];   
$clave = substr($razon_social, 0, 3);
$cif_nif = $form_state['storage']['field_cif_dni'];

$usuario = $clave.$cif_nif;
$password = password(8);
//$imagen = $form_state['values']['field_logo_empresa'];

$roles = user_roles();
$newuser = array(
        'name' =&gt; $usuario,
        'pass' =&gt; $password,
        'mail' =&gt; $mail,
        'status' =&gt; 1,
        'roles' =&gt; array('7' =&gt; 'empresa'),
        'access' =&gt; time(),

);
$existing_user = user_load($usuario);
if (!$existing_user-&gt;uid) {
    $account = user_save(null, $newuser);

    $profile = entity_create('profile2', array('type' =&gt; 'empresa'));
    $profile-&gt;uid = $account-&gt;uid;

    //Datos a guardar para el perfil

    $profile-&gt;field_nombre_contacto[LANGUAGE_NONE][0]['value'] = $nombre;
    $profile-&gt;field_apellidos_contacto[LANGUAGE_NONE][0]['value'] = $apellidos;
    $profile-&gt;field_razon_social[LANGUAGE_NONE][0]['value'] = $razon_social;
    $profile-&gt;field_cif_dni[LANGUAGE_NONE][0]['value'] = $cif_nif;
    $profile-&gt;field_direccion[LANGUAGE_NONE][0]['value'] = $direccion;
    $profile-&gt;field_codigo_postal[LANGUAGE_NONE][0]['value'] = $codigo_postal;
    $profile-&gt;field_telefono[LANGUAGE_NONE][0]['value'] = $telefono;
    $profile-&gt;field_web[LANGUAGE_NONE][0]['value'] = $web;
    $profile-&gt;field_poblacion[LANGUAGE_NONE][0]['value'] = $poblacion;
    $profile-&gt;field_comunidad_autonoma[LANGUAGE_NONE][0]['value'] = $comunidad_autonoma;
    $profile-&gt;field_provincia[LANGUAGE_NONE][0]['value'] = $provincias;
    $profile-&gt;field_municipio[LANGUAGE_NONE][0]['value'] = $municipios;

    $profile-&gt;save();

    $account-&gt;profiles['empresa'] = $profile;
    entity_save('user', $account);
    drupal_goto('user');

}
}


My question is: How could I set the fid from the table and assign it to the user?
@Luism, you should use field widget for it. field_attach_form attaches fields to form, field_attach_form_validate validates all attached fields, field_attach_submit prepared entity to be stored. Regarding values in $form_state['values'] you will have big structure inside $form_state['values']['FIELD_NAME'][LANGUAGE][INDEX] but you need only $form_state['values']['FIELD_NAME'][LANGUAGE][INDEX]['fid'], it will contain File ID from file_managed table (with status=0) for uploaded files and after profile2_save() file will be activated or deleted otherwise.