1. 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

Discussion in 'XenForo Development Discussions' started by Lyphiard, May 23, 2015.

  1. Lyphiard

    Lyphiard Active Member

    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.
     
  2. Brogan

    Brogan XenForo Moderator Staff Member

    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.
     
  3. Jake B.

    Jake B. Well-Known Member

    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
     
  4. Lyphiard

    Lyphiard Active Member

    Thanks!

    Is there any way I can set the avatar?
     
  5. Jake B.

    Jake B. Well-Known Member

    Look around in XenForo_Model_Avatar and XenForo_ControllerPublic_Account
     
  6. Lyphiard

    Lyphiard Active Member

    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
     
  7. Jake B.

    Jake B. Well-Known Member

    Well, if this is something you plan on releasing you should probably not use file_get_contents on a url, but instead use cURL
     
  8. Lyphiard

    Lyphiard Active Member

    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.
     
  9. Jake B.

    Jake B. Well-Known Member

    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?
     
  10. Lyphiard

    Lyphiard Active Member

    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?
     

Share This Page