0

I can't figure out how to add image to a custom image field on a sepcific user.

    $f = file_save_data($user['thumbnailphoto'][0], 'private://profile/'. $data['name'] .'.jpg', FILE_EXISTS_REPLACE);
    $fid = $f->fid;

    //Add to virtual map 'Profile images'
    $file_entity = file_load($fid);
    $file_entity->status = 1;
    $file_entity->field_folder['und'][0]['tid'] = 180;
    file_save($file_entity);

    $user = user_load(334);

    $details['field_profile_picture'] = array(
                                        'und' => array( 
                                            0 => array(
                                            'fid' => $fid,
    )));

    $u = user_save($user, $details);

But nothing get saved? Field_profile_picture is just an image field.

Thx!

4
  • 1
    Probably not causing your problem, but you should never hardcode in 'und' for language. Instead use the defined constant, LANGUAGE_UNDEFINED or a $language variable.
    – beth
    Commented Aug 25, 2014 at 15:42
  • Have you tried using Devel and dpm after every file_save, user_load, user_save call to be sure the actions are being performed?
    – tenken
    Commented Aug 25, 2014 at 17:50
  • I'll try ;). Thx!
    – mgoubert
    Commented Aug 26, 2014 at 7:16
  • LANGUAGE_UNDEFINED did the trick, thx!
    – mgoubert
    Commented Aug 26, 2014 at 9:48

1 Answer 1

0

In Drupal 7 you can use system_retrieve_file()

system_retrieve_file : This function will download a file from a remote source, copy it from temp to a specified destination and optionally save it to the file_managed table if you want it to be managed.


  $usr_info = user_load(334); //load user object
  if (!empty($usr_info)) {//assign only if user exist
    $image_url = 'http://g-raph.be/sites/all/themes/g_raph_/images/image03.jpg'; //image URL to save. If image already saved in the drupal system then get the file ID(fid)
    //I have used picture folder to store images using image field settings
    $file_info = system_retrieve_file($image_url, 'public://pictures/', TRUE, FILE_EXISTS_REPLACE); //storing image inside picture folder
    if ($file_info->fid) {//if fid exist then image is saved from URL.
      //$usr_info->picture = file_load($file_info->fid); //for default profile pic
      $usr_info->field_profile_picture[LANGUAGE_NONE][0]['fid'] = $file_info->fid; //for custom profile pic
    }
    user_save($usr_info);
  }
6
  • Sorry but this is not answer to my question, the question would be: How to download a file from a remote source and save it to a custom user field :p. But thx annyway ;)
    – mgoubert
    Commented Aug 26, 2014 at 7:15
  • Your are trying to assign a profile picture to a particular user right?
    – Inizio
    Commented Aug 26, 2014 at 7:26
  • What is the value of $user['thumbnailphoto'][0]?
    – Inizio
    Commented Aug 26, 2014 at 7:30
  • i should have called it $image, its an variable containing the image :) data. But it's not important, the picture get's saved correctly, the fid is just not saved on the custom field.
    – mgoubert
    Commented Aug 26, 2014 at 7:36
  • If details is an object then try to use : $details->field_profile_picture['LANGUAGE_NONE'][0]['fid'] = $fid;. Can you provide me more details on $details variable/object?
    – Inizio
    Commented Aug 26, 2014 at 9:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.