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;