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

Setting a user's avatar

Discussion in 'XenForo Development Discussions' started by Robust, May 3, 2015.

  1. Robust

    Robust Well-Known Member

    Is there any way to set a user's avatar directly to an external image?
  2. Chris D

    Chris D XenForo Developer Staff Member

    You would need to download the image to a temporary file, then pass that in to the various default avatar functions on the avatar model which will handle the resizing, copying and ultimate setting of the new avatar.

    Effectively you can use XF code for everything except the initial bit (which will involve downloading vs uploading the avatar).
  3. Robust

    Robust Well-Known Member

    So something like this should work?

    $url = 'http://example.com/image.png';
    $filename = '/temp/myfolder';
    file_put_contents($filename, file_get_contents($url));
    There's also the simple:

    That requires allow_url_fopen set to true though. I can always use cURL too, which is required for my add-on anyway. Which would be the recommended method of downloading the file? And if I use the slash (/) is that relative to the XenForo installation or the root of the server?
  4. Chris D

    Chris D XenForo Developer Staff Member

    More or less.

    This is typically what XF does to create temporary files:
    $filename tempnam(XenForo_Helper_File::getTempDir(), 'xf');
    Robust likes this.
  5. Robust

    Robust Well-Known Member

    How do you download them initially? I don't think XF requires cURL as per the requirements page, and it doesn't explicitly ask for allow_url_fopen to be set to true. They're the only two ways I know to download files.
  6. Chris D

    Chris D XenForo Developer Staff Member

    Use XenForo_Helper_Http

Share This Page