I'm trying to update the user avatar in my custom module. Which method shall I use to update the image in db as well as to move the file to public directory?
The file gets stored to
file_managed table like this:
// Programmatically create files. use Drupal\file\Entity\File; $file = File::create([ 'uid' => 1, 'filename' => $_FILES['file_id']['name'], 'uri' => 'public://pictures/' . $_FILES['file_id']['name'], 'status' => 1, ]); $file->save();
And I manually updated
user__user_picture table which is referring to the
fid in the above table.
Finally I ended up with
move_uploaded_file to store the image to the filesystem.
I'll proceed with this approach until I find a better way to do the same.
$user = \Drupal\user\Entity\User::load($uid); $user->set('user_picture', $fid); $user->save();