1

i wrote the program belowed

$fields=array('name'=>'test user','picture'=>$fid);

$account=user_save($account, $fields);

i really changed the user name but i can't change the picture field in the users table

what could the problem be?

by the way, i also use db_update. The situation is the same

the code is blowed

db_update('users')
->condition('uid',$account->uid)
->fields(array('picture'=>$file->fid))
->execute();

still not work I hope somebody can help me,Thanks!

0

hybrydauth module does somethink like this

$file = new stdClass();
  $file->uid      = $account->uid;
  $file->status   = 0; // mark the file as temporary
  $file->filename = basename($tmp_photo_realpath);
  $file->uri      = $tmp_photo;
  $file->filemime = $info['mime_type'];
  $file->filesize = $info['file_size'];

  // The file should be an image
  $errors = array();
  $errors += file_validate_is_image($file);
  $errors += file_validate_image_resolution($file, variable_get('user_picture_dimensions', '85x85'));
  $errors += file_validate_size($file, variable_get('user_picture_file_size', '30') * 1024);

  // Make sure file extension is a valid image
  if (!in_array(strtolower($info['extension']), array('jpg', 'png', 'gif'))) {
    $errors[] = ' invalid image file extension.';
  }

  if (count($errors)) {
    drupal_set_message(t('Profile Image Import:') . ' ' . $errors[0], 'warning');
    // Clean up (set fid to avoid error messages)
    $file->fid = 0; file_delete($file);
  }
  else {
    // We'll need a valid file id on the file object; file_save() will give us one
    $file = file_save($file);
    // Update user account (fid is not empty, status is temporary -- image
    // will be moved to proper directory and assigned to the user)
    $fields['picture'] = $file;
    $account = user_save($account, $fields);
  • thx for providing this code, i actually just used this code and doesn't work! – cobenash Jul 2 '12 at 6:00
2

Try this code

 $file = file_load($fid);
 $file->status = 0;
 $edit = array ('picture' => $file);
 $u = user_save($account,$edit); 
  • $file->status = 0; should be $file->status = 1; – Rajveer gangwar Mar 27 '18 at 8:57
0

This is done in fboauth module

  // Update the user record.
  $picture_file->uid = $account->uid;
  $picture_file = file_save($picture_file);
  file_usage_add($picture_file, 'user', 'user', $account->uid);
  db_update('users')
    ->fields(array(
    'picture' => $picture_file->fid,
    ))
    ->condition('uid', $account->uid)
    ->execute();

you can use this code you just have to load file $picture_file first using file_load

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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