We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 28615
    • 14 Posts
    I am using the following snippet as a postHook in Register snippet to add Photo (image of the the user) in registration. Code works, but error is coming the image is not found (404 error).

    <?php
    $profile = $hook->getValue('register.profile');  
    // get the array of extended user profile attributes
    $extended = $profile->get('extended');
    
    // my register form value is "authorPhoto" and it is file type input 
    $extended['authorPhoto'] => true;
    
    $profile->set('extended', $extended);
    $profile->save();
    


    and retrieving the extended field values like these inside my other documents
    <?php
    // get the array of extended user profile attributes
    $profile = $modx->user->getOne('Profile');
    $modx->toPlaceholders($profile->get('extended'),'userext');


    by using
    [[+userext.authorPhoto]]


    Can somebody what causes this error? Is there any better way to add image file in to modx user profile? Thanks in Advance.

    Regards
    Manikandan
      • 3749
      • 24,544 Posts
      You don’t appear to be setting a path to the image anywhere. I think you wan that path instead of ’true’.

        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 28615
        • 14 Posts
        I tried something like this below

        $extended['authorPhoto'] => 'assets/userimg/authorPhoto';


        instead of old code
        $extended['authorPhoto'] => true;
        pointing to true.

        But the problem persists. Since image is coming via form, the above path is right?
          • 3749
          • 24,544 Posts
          Quote from: stmani at Nov 15, 2010, 09:01 PM

          I tried something like this below

          $extended['authorPhoto'] => 'assets/userimg/authorPhoto';


          It should look more like this:

          $extended['authorPhoto']=>MODX_ASSETS_PATH . 'userimg/authorphoto.jpg';


          FYI, it’s a good practice to put all file and folder names in all lower case to avoid case-sensitivity issues.

          What code are you using to display the photo?
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 28615
            • 14 Posts
            Thanks Bobroy for helping. I tried your suggestion.
            $extended['authorPhoto'] => MODX_ASSETS_PATH.'userimg/authorPhoto';


            But it is not working. Since ’authorPhoto’ is a variable name coming from registration form, I name it with capital letter in middle.

            I am displaying it with following code
            <?php
            // get the array of extended user profile attributes
            $profile = $modx->user->getOne('Profile');
            $modx->toPlaceholders($profile->get('extended'),'userext');


            with [[+userext.authorPhoto]] inside the html. When inspect the html page it shows the file name (mani14.jpg) but server not found the file. I think it is related with how this file is stored (or referred) in moduser table in database. I am able to get all other profile values (including extented values) in my web page except photo.

            Thanks
            Manikandan
              • 3749
              • 24,544 Posts
              Two options, then:

              1. make it save the path to the file in the DB.

              2. make the HTML:

              <img src="[[++base_url]path/to/image/directory/[[+userext.authorPhoto]]">
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 28615
                • 14 Posts
                Thanks BabRay. I finally managed in a different way. Instead of struggling with registration form, I removed the options there. Once user registered, they can upload their image within the website. I stored the image files in some folders and appended the files name to Modx user database table (in the extended fields) as suggested you.Following is the code.

                <?php
                //assigning image file name to user database
                $profile = $modx->user->getOne('Profile');
                
                // get the array of extended user profile attributes
                $extended = $profile->get('extended');
                
                // add the value of the element for key `authorPhoto`
                
                $extended['authorPhoto'] = $_FILES['userfile']['name'];
                $profile->set('extended', $extended);
                $profile->save();


                It is working.with the html <img src="[[++base_url]path/to/image/directory/[[+userext.authorPhoto]]">..

                As a followup, I decided that uploadImageform chunk should be shown to those who is not updated their image.So I tried the following snippet.

                <?php
                $profile = $modx->user->getOne('Profile');
                if ($profile)
                {
                $extended=$profile->get('extended');
                if ($extended['authorPhoto']==0)
                {
                $output = $modx->getChunk($uploadImage_show_option); 
                return $output;
                }}


                It seems some problem with if condition statement above.The form is not load for both the cases. I am trying to rectify that. Any obvious errors?

                Manikandan
                  • 3749
                  • 24,544 Posts
                  Try:

                  if (empty($extended['authorPhoto']))
                    Did I help you? Buy me a beer
                    Get my Book: MODX:The Official Guide
                    MODX info for everyone: http://bobsguides.com/modx.html
                    My MODX Extras
                    Bob's Guides is now hosted at A2 MODX Hosting
                    • 28615
                    • 14 Posts
                    It is working. It is problem with the parsing the chunk to the snippet and I used this code
                    if (empty($extended['authorPhoto']))


                    Thanks
                    Manikandan
                      • 3749
                      • 24,544 Posts
                      Glad to hear you got it working. smiley
                        Did I help you? Buy me a beer
                        Get my Book: MODX:The Official Guide
                        MODX info for everyone: http://bobsguides.com/modx.html
                        My MODX Extras
                        Bob's Guides is now hosted at A2 MODX Hosting