• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Changing a username and profile picture

Lyphiard

Active member
#1
Currently, I found out that the easiest way to change a username was to update `xf_user` and set the username to the new username, which then will update across the board accordingly.

However, is this the best way to do it though?

Also, I need to be able to change their profile picture to something else. I have their new profile picture at https://example.com/profiles/<username>/, but I cannot seem to find any way to update their profile picture.

Thanks.
 

Brogan

XenForo moderator
Staff member
#2
Are you writing an add-on?

If so, you should look at how user name changes are handled in code when updated via the ACP.
 

Jake B.

Well-known member
#3
You should definitely use the DataWriter to do things like this:

Code:
$userWriter = XenForo_DataWriter::create('XenForo_DataWriter_User');
$userWriter->setExistingData($userId);
$userWriter->set('username', $newUsername);
$userWriter->save();
as there is more that may end up changing than just the 'username' field in xf_user.

Regards,

Jake
 

Lyphiard

Active member
#6
This is what I have:
Code:
        $visitor = XenForo_Visitor::getInstance();
        $tempFileName = md5(mt_rand(1, 99999999) . time()) . "-fv-avatar.png";
        file_put_contents("/tmp/" . $tempFileName, file_get_contents("https://example.com/avatar/" . $visitor['username']));
        $avatarModel = XenForo_Model::create('XenForo_Model_Avatar');
        $source = array(
            "avatar" => array(
                "name" => $visitor['user_id'] . ".png",
                "tmp_name" => "/tmp/" . $tempFileName,
            ),
        );
        $avatar = XenForo_Upload::getUploadedFile('avatar', $source);
        $avatarData = $avatarModel->uploadAvatar($avatar, $visitor['user_id'], $visitor->getPermissions());
It works correctly from what I can see, is there anything I can do to improve it?
Thanks
 

Jake B.

Well-known member
#7
Well, if this is something you plan on releasing you should probably not use file_get_contents on a url, but instead use cURL
 

Lyphiard

Active member
#8
This is just a private thing I'm doing for my forum. I tried using cURL first, but the picture kept on coming back corrupt with nothing inside.
 

Jake B.

Well-known member
#9
This is just a private thing I'm doing for my forum. I tried using cURL first, but the picture kept on coming back corrupt with nothing inside.
It's still a better optiont o use cURL. What was the code you were using, and where are you trying to get the image from?
 

Lyphiard

Active member
#10
I eventually stumbled into this error: An exception occurred: Your avatar's file size is too large. Please upload an avatar no bigger than 0 bytes.

I have the forum on default settings, and uploading an avatar manually works.

Any ideas?