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?
Thanks.
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?
Thanks.
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.
http://www.drupal8.ovh/en/tutoriels/47/create-a-file-drupal-8
$user = \Drupal\user\Entity\User::load($uid);
$user->set('user_picture', $fid);
$user->save();
use Drupal\file\Entity\File;
to update user picture. Simply load the user by their uid
and pass the fid
or form file upload id in above code. It will automatically update the user profile picture.
– Manav
Jan 22 '18 at 12:23
file_save_data()
,what should be given as the first parameter? As shown here (api.drupal.org/api/drupal/core!modules!file!file.module/…) the parameter should be string $data: A string containing the contents of the file. Is this the id of the file type? – wilNev Apr 14 '16 at 9:32