We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 42753
    • 17 Posts
    Hi everyone! I'm using Avatar 1.0.0-pl

    When I try to upload an avatar, I get error:

    Warning: mkdir() [function.mkdir]: No such file or directory in /var/www/yan/data/www/mmonstr.ru/core/cache/includes/elements/modsnippet/21.include.cache.php on line 115

    I think the reason is that there is no such a folder on the server, where the script wants to upload an image. But I can't understand, where this folder must be located and how it must be named. I looked at the script's source code, but did't find that path.

    Here's the source code of the snippets:

    1. avatar.upload:

    <?php
    /**
    Avatar
    
    Copyright 2011 by Yuri Kobets <[email protected]>
    
    Avatar is free software; you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by the Free
    Software Foundation; either version 2 of the License, or (at your option) any
    later version.
    
    Avatar is distributed in the hope that it will be useful, but WITHOUT ANY
    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
    A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    
    @package avatar
    */
    
    /**
    MODx avatar.upload Snippet
    
    With this snippet your users can upload avatars for the profile
    
    @package avatar
    
    @property textfield defavatar: the url to the default avatar.
    @property textfield maxwidth: the maximum width of the avatar.
    @property textfield maxheight: the maximum height of the avatar.
    
    Snippet sets the placeholders:
    uploadavatar.update_error: 1 if there is an error 0 if successfully updated
    uploadavatar.error: error text
    uploadavatar.avatarurl: url to the avatar. You can use this url in the img src
    
    */
    
    if(!$modx->user->isAuthenticated())
    {
        return '';
    }
    
    $defavatar  = $modx->getOption('defavatar',   $scriptProperties,    '/assets/images/avatars/default.png');
    $maxwidth   = $modx->getOption('maxwidth',    $scriptProperties,    100);
    $maxheight  = $modx->getOption('maxheight',   $scriptProperties,    100);
    
    $userspath = $modx->getOption('avatar_users_path');
    $usersurl = $modx->getOption('avatar_users_url');
    
    $searchArray = array(
        '{core_path}',
        '{assets_path}',
        '{base_path}',
    );
    $replacePaths = array(
        $modx->getOption('core_path',null,MODX_CORE_PATH),
        $modx->getOption('assets_path',null,MODX_ASSETS_PATH),
        $modx->getOption('base_path',null,MODX_BASE_PATH),
        );
    
    $userspath = str_replace($searchArray, $replacePaths, $userspath);
    
    
    $modx->lexicon->load('avatar:default');
    
    $profile = $modx->user->getOne('Profile');
    if(empty($profile))
    {
        return '';
    }
    
    $params = $modx->request->getParameters(array(), 'POST');
    
    $modx->setPlaceholder("uploadavatar.update_error", 0);
    
    if(!empty($params['upload-btn']))
    {
        if($_FILES['uploadfile']['size'] != 0)
        {
            $iserror = 0;
            $avatarfile = '';
            $fileInfo = pathinfo(basename($_FILES['uploadfile']['name']));
            if(strtolower($fileInfo["extension"]) != "png" &&
               strtolower($fileInfo["extension"]) != "jpg" &&
               strtolower($fileInfo["extension"]) != "gif" &&
               strtolower($fileInfo["extension"]) != "jpeg")
            {
                $modx->toPlaceholder('uploadavatar.error', $modx->lexicon('avatar.notimage'));
                $iserror = 1;
            } else
            {
                $imgSize = getimagesize($_FILES['uploadfile']['tmp_name']);
                if(is_array($imgSize))
                {
                    if($imgSize[0] > $maxwidth || $imgSize[1] > $maxheight)
                    {
                        $errmsg = sprintf($modx->lexicon('avatar.errdim'), $maxwidth . 'x' . $maxheight);
                        $modx->setPlaceholder("uploadavatar.error",  $errmsg);
                        $iserror = 1;
                    }
                } else
                {
                    $modx->setPlaceholder("uploadavatar.error", $modx->lexicon('avatar.notimage'));
                    $iserror = 1;
                }
                if(!$iserror)
                {
                    $saveto = $userspath . '/' . $modx->user->get('id');
                    if(!file_exists($saveto))
                    {
                        if(!mkdir($saveto))
                        {
                            $modx->setPlaceholder("uploadavatar.error", $modx->lexicon('avatar.errinternal'));
                            $iserror = 1;
                        }
                    }
                    if(!$iserror)
                    {
                        $oldphoto = $profile->get('photo');
                        
                        $avatarfile = 'avatar.' . $fileInfo["extension"];
                        $oldphotopath = $saveto . '/' . $oldphoto;
                        $saveto = $saveto . '/' . $avatarfile;
                        if(file_exists($_FILES['uploadfile']['tmp_name']))
                        {
                            if(!move_uploaded_file($_FILES['uploadfile']['tmp_name'], $saveto))
                            {
                                $modx->setPlaceholder("uploadavatar.error", $modx->lexicon('avatar.errinternal'));
                                $iserror = 1;
                            } else if(!empty($oldphoto) && $oldphoto != $avatarfile)
                            {
                                if(file_exists($oldphotopath))
                                {
                                    unlink($oldphotopath);
                                }
                            }
                        } else
                        {
                            $modx->setPlaceholder("uploadavatar.error", $modx->lexicon('avatar.errinternal'));
                            $iserror = 1;
                        }
                    }
                }
            }
            if(!$iserror)
            {
                $avatarurl = $avatarfile;
                $profile->set('photo', $avatarurl);
                $profile->save();
                $modx->setPlaceholder("uploadavatar.update_error", 0);
            } else
            {
                $modx->setPlaceholder("uploadavatar.update_error", 1);
            }
        } else
        {
            if(!empty($params['delete']))
            {
                $saveto = $userspath . '/' . $modx->user->get('id') . '/' . $profile->get('photo');
                if(file_exists($saveto))
                {
                    unlink($saveto);
                }
                
                $profile->set('photo', '');
                $profile->save();
            }
        }
    }
    
    $photo = $profile->get('photo');
    
    if(!empty($photo))
    {
        $configs = $modx->getConfig();
        $avatarurl = $configs['site_url'] . $usersurl . '/' . $modx->user->get('id') . '/' . $photo;
        $modx->setPlaceholder('uploadavatar.avatarurl', $avatarurl);
    } else
    {
        $modx->setPlaceholder('uploadavatar.avatarurl', $defavatar);
    }


    2. avatar.url:

    <?php
    /**
    Avatar
    
    Copyright 2011 by Yuri Kobets <[email protected]>
    
    Avatar is free software; you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by the Free
    Software Foundation; either version 2 of the License, or (at your option) any
    later version.
    
    Avatar is distributed in the hope that it will be useful, but WITHOUT ANY
    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
    A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    
    @package avatar
    */
    
    /**
    MODx avatar.url Snippet
    
    This the Output Filter can be used to show the user avatar
    Usage:
    
    <img src="[[+createdby:avatar.url=`assets/images/defavatar.png`]]">
    
    input string is the default avatar url.
    */
    
    $user = $modx->getObject('modUser', array('id' => $input));
    if(empty($user))
    {
        return $options;
    }
    $profile = $user->getOne('Profile');
    if(empty($profile))
    {
        return $options;
    }
    
    $photo = $profile->get('photo');
    if(!empty($photo))
    {
        $configs = $modx->getConfig();
        $usersurl = $modx->getOption('avatar_users_url');
        return $configs['site_url'] . $usersurl . '/' . $modx->user->get('id') . '/' . $photo;
    } else
    {
        return $options;
    }
    [ed. note: webstyle last edited this post 13 years, 7 months ago.]
      • 34101
      • 3 Posts
      There are two variables in the ModX System Settings for Avatar:

      avatar_users_path - path where upload avatars (by default: {assets_path}/users)
      avatar_users_url - url to the folder with avatars (by default: assets/users)

      You have to create the folder in "avatar_users_path".
        • 42753
        • 17 Posts
        Quote from: kobets at Feb 13, 2013, 04:51 AM
        There are two variables in the ModX System Settings for Avatar:

        avatar_users_path - path where upload avatars (by default: {assets_path}/users)
        avatar_users_url - url to the folder with avatars (by default: assets/users)

        You have to create the folder in "avatar_users_path".

        Thank you! I've created assets/users folder and it works. But now I have another problem: at the profile page I want to show user's avatar. I use the output filter from avatar.url snippet's example:

        [[!avatar.url]]
        <img src="[[+createdby:avatar.url=`assets/users/default-avatar.png`]]">


        and get the following errors:

        Notice: Undefined variable: input in .../core/cache/includes/elements/modsnippet/22.include.cache.php on line 35

        Notice: Undefined variable: options in .../core/cache/includes/elements/modsnippet/22.include.cache.php on line 38

        Update:

        These errors are caused by [[!avatar.url]]

        The output filter
        <img src="[[+createdby:avatar.url=`assets/users/default-avatar.png`]]">

        itself always shows the default avatar even if the avatar is set.

        While
        [[!avatar.upload]]
        <img src="[[+uploadavatar.avatarurl]]">

        works normally. But it doesn't show the default avatar if it wasn't set. [ed. note: webstyle last edited this post 13 years, 7 months ago.]
          • 34101
          • 3 Posts
          I've updated package. The bug in avatar.url is fixed.
            • 42753
            • 17 Posts
            Quote from: kobets at Feb 13, 2013, 07:06 PM
            I've updated package. The bug in avatar.url is fixed.

            Installed new version - nothing changed.

            [[!avatar.url]]

            still returns the errors and
            <img src="[[+createdby:avatar.url=`assets/users/default-avatar.png`]]">

            still returns default avatar.
              • 34101
              • 3 Posts
              Firstly you don't need [[!avatar.url]]. avatar.url can't be used as snippet. I guess this is why it shows error.

              the code below is just an example
              <img src="[[+createdby:avatar.url=`assets/users/default-avatar.png`]]">


              Actually you have to replace "+createdby" with something that contain the userid. If you are using avatar.url in chunk, be sure the placeholder +createdby is valid. If you are using avatar.url on page or in the template and want to show the avatar of user who created the page then use it as :

              <img src="[[*createdby:avatar.url=`assets/users/default-avatar.png`]]">


              or

              <img src="[[*publishedby:avatar.url=`assets/users/default-avatar.png`]]">

                • 42753
                • 17 Posts
                I use it in a chunk.

                <img src="[[+id:avatar.url=`assets/users/default-avatar.png`]]">

                works fine.

                Thank you!
                  • 42753
                  • 17 Posts
                  I think I've found one more bug in the snippet.

                  When I use

                  <img src="[[+id:avatar.url=`assets/users/default-avatar.png`]]">


                  inside of "Peoples" snippet's chunk it returns

                  <img src="assets/users/0/avatar.png`]]">


                  for all users with uploaded avatars.

                  [[+id]] itself works normally.

                  Had to use
                  <img src="assets/users/[[!If? subject=`[[+photo]]` &operator=`notempty` &then=`[[+id]]/[[+photo]]` &else=`default-avatar.png`]]">

                  for now.

                  Update. This is not Avatar's bug, but the problem of using snippets inside of Peoples's chunk. [ed. note: webstyle last edited this post 13 years, 7 months ago.]
                    • 41101
                    • 40 Posts
                    Ho do you make this work?

                    Do you have to create your own form and set the defavatar field or should you just be able to place [[avatar.upload]] in a resource?
                      • 47589
                      • 6 Posts
                      I second Jonboys comment. Is there any documentation for this snippet?