We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 8168
    • 1,118 Posts
    Hi guys.

    I am uploading a file to an extended doc titled "cvdoc" in the MODx user profile and have that working well. I have a posthook snippet set to move the uploaded file from a temporary folder ("assets/temp/") to a new unique user folder ("assets/profiles/USERID/") which again works - the element not working in the pasted code below, is that the url path saved in the 'cvdoc' profile field is not updated when the image is moved... It stays as the "assets/temp/filename" one... Can anyone help fix it?

    Code taken from here btw > http://www.virtudraft.com/blog/upload-photo-in-modx-user-registration.html

    Cheers,

    /** @var modUser $user */
        $user = & $fields['register.user'];
        $userId = $user->get('id');
        /** @var modUserProfile $profile  */
        $profile = & $fields['register.profile'];
        if (!empty($fields['cvdoc'])) {
            $cvdoc = array();
            $cvdoc['temp'] = $fields['cvdoc'];
            $cvdoc['basename'] = basename($cvdoc['temp']);
            /***********************************************************************
             * XXX: IMPORTANT XXX
             *
             * Create unique path here for this profile updating.
             * You can change this as you wish.
             * The $userId variable comes from above initiation.
             *
             ***********************************************************************/
            $cvdoc['newdir'] = 'assets/uploads/profiles/' . $userId . '/';
            $cvdoc['newfilepath'] = $cvdoc['newdir'] . $cvdoc['basename'];
            $cvdoc['target'] = $hook->modx->config['base_path'] . $cvdoc['temp'];
            $cvdoc['moved'] = $hook->modx->config['base_path'] . $cvdoc['newfilepath'];
            // make the user's private directory
            mkdir($cvdoc['newdir'], 0755, true);
            $photoUpdated = false;
            // move the photo from the temporary path to the new one
            if (!rename($cvdoc['target'], $cvdoc['moved'])) {
                // if "rename" function fails, try "copy" instead.
                if (!copy($cvdoc['target'], $cvdoc['moved'])) {
                    // just dump the log report to the MODX's error log,
                    // because both "rename" and "copy" functions fail
                    $hook->modx->log(modX::LOG_LEVEL_ERROR, __FILE__ . ' ');
                    $hook->modx->log(modX::LOG_LEVEL_ERROR, __LINE__ . ': $userId ' . $userId);
                    $hook->modx->log(modX::LOG_LEVEL_ERROR, __LINE__ . ': $cvdoc ' . print_r($cvdoc, 1));
                } else {
                    // if copy succeeded, delete the old temporary picture
                    unlink($cvdoc['target']);
                    $photoUpdated = true;
    $profile->set('cvdoc', $cvdoc['newfilepath']);
                $profile->save();
                }
            } else {
                $photoUpdated = true;
            }
            if ($photoUpdated) {
                /**
                 * Now we update the profile
                 * The $profile variable comes from above initiation.
                 */
                $profile->set('cvdoc', $cvdoc['newfilepath']);
                $profile->save();
                /**
                 * Yeah! xPDO rocks! Simply like that!
                 */
            }
        }
    
    
    
    
        return TRUE;
    
      • 8168
      • 1,118 Posts
      Anyone got any ideas on this one???? Its baffling me!
        • 3749
        • 24,544 Posts
        The profile doesn't have a cvdoc field, so it's not being set. If that's an extended field, you'd have to do something like this:

        $extended = $profile->get('extended');
        $extended['cvdoc'] = $cvdoc['newfilepath'];
        $profile->set('extended', $extended);
        $profile->save();


        This all assumes that your $profile variable holds the modUserProfile object. I think it does, but I'm not sure. If it does, I don't see any need to save the user object, since it look like you're only modifying the profile. You should be able to do just $profile->save();
          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
          • 8168
          • 1,118 Posts
          Quote from: BobRay at Sep 21, 2015, 08:48 PM
          The profile doesn't have a cvdoc field, so it's not being set. If that's an extended field, you'd have to do something like this:

          $extended = $profile->get('extended');
          $extended['cvdoc'] = $cvdoc['newfilepath'];
          $profile->set('extended', $extended);
          $profile->save();


          This all assumes that your $profile variable holds the modUserProfile object. I think it does, but I'm not sure. If it does, I don't see any need to save the user object, since it look like you're only modifying the profile. You should be able to do just $profile->save();

          Thanks Bob... but afraid I am rather clueless on how to integrate your solution in that which I am using currently... I had a go at replacing the current set / save function but it made matters worse... The file is being saved to the server well by the original code, all that is not working is that once the file is moved from its temp location to its unique location, the cvdoc field is not being updated to reflect the server path change... that's all that needs resolving... Any ideas?
            • 8168
            • 1,118 Posts
            Bob... tell I lie! If I add that in after the current set / save code.... it works! Thanks wink