Skip to content

Instantly share code, notes, and snippets.

@dubrod
Last active October 7, 2016 11:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dubrod/3ab074ce97cffff38d09e0761500ef39 to your computer and use it in GitHub Desktop.
Save dubrod/3ab074ce97cffff38d09e0761500ef39 to your computer and use it in GitHub Desktop.
4 part MODX User Profile Upload. Not 1 large script.
// == Script Origin and Discussion
// https://forums.modx.com/thread/?thread=80740&page=2
// Used Oct 2016 on MODX 2.5
// == Form Markup
[[!UpdateProfile? &validate=`fullname:required,email:required:email` &preHooks=`UploadProfilePhoto`]]
<label for="Profile_Photos">Profile Photo <span class="error">[[!+fi.error.Profile_Photos]]</span></label>
<img src="[[+Profile_Photos:phpthumbof=`w=210&h=210`]]" />
<input id="Profile_Photos" name="Profile_Photos" type="file" value="[[+fi.Profile_Photos]]" maxlength="100000" />
// == UploadProfilePhoto
<?php
//$modx->log(modX::LOG_LEVEL_ERROR,'Start of script-');
// initialize output;
$output = true;
// get the current user name to for dicroty placement
$userName = $modx->user->get('username');
// valid extensions
$ext_array = array('jpg', 'jpeg', 'gif', 'png');
// create unique path for this form submission
$uploadpath = 'assets/userfiles/' . $userName .'/';
// get full path to unique folder
$target_path = $modx->config['base_path'] . $uploadpath;
// get uploaded file names:
$submittedfiles = array_keys($_FILES);
//get exsisting user profile data for Profile_Photos
$fields = $modx->user->getOne('Profile')->get('extended');
$Photo_fields = $fields['Profile_Photos'];
//$modx->log(modX::LOG_LEVEL_ERROR,'what is the user profile photo -'.$fields['Profile_Photos']);
//$modx->log(modX::LOG_LEVEL_ERROR,'linsting var is the user profile photo -'.$Photo_fields);
// loop through files
foreach ($submittedfiles as $sf) {
// Get Filename and make sure its good.
$filename = basename( $_FILES[$sf]['name'] );
// Get file's extension
$ext = pathinfo($filename, PATHINFO_EXTENSION);
$ext = mb_strtolower($ext); // case insensitive
//$modx->log(modX::LOG_LEVEL_ERROR,'in for loop file namnes are?-'.$filename );
if($filename != '') {
//$modx->log(modX::LOG_LEVEL_ERROR,'in file name loop'.$filename);
// is this the right type of file?
if(in_array($ext, $ext_array)) {
//create file called the filename that has been sanitized
$modx->log(modX::LOG_LEVEL_ERROR,'before sanitization'.$filename);
$filename = strtolower(preg_replace("/[^A-Za-z0-9.]+/i", "-", $filename));
//$filename = $filename . '.'.$ext ;
//$modx->log(modX::LOG_LEVEL_ERROR,'after sanitization and.ext'.$filename);
// full path to new file
$myTarget = $target_path . $filename;
// create directory to move file into if it doesn't exist
mkdir($target_path, 0755, true);
if(file_exists($myTarget)) {
chmod($myTarget,0755); //Change the file permissions if allowed
unlink($myTarget); //remove the file
}
// is the file moved to the proper folder successfully?
if(move_uploaded_file($_FILES[$sf]['tmp_name'], $myTarget)) {
// set a new placeholder with the new full path (if you need it in subsequent hooks)
$hook->setValue($sf, $uploadpath . $filename);
// set the permissions on the file
if (!chmod($myTarget, 0644)) { /*some debug function*/ }
}
else {
// File not uploaded
//$modx->log(modX::LOG_LEVEL_ERROR,'here was a problem uploading the file? -'.$Photo_fields);
$errorMsg = 'There was a problem uploading the file.';
$hook->addError($sf, $errorMsg);
$output = false; // generate submission error
}
}
else {
// File type not allowed
//$modx->log(modX::LOG_LEVEL_ERROR,'Type of file not allowed. -'.$Photo_fields);
$errorMsg = 'Type of file not allowed.';
$hook->addError($sf, $errorMsg);
$output = false; // generate submission error
//$modx->log(modX::LOG_LEVEL_ERROR,'generate submission error');
}
//$modx->log(modX::LOG_LEVEL_ERROR,'Completed we have a valif photo name'.$Photo_fields);
//$modx->log(modX::LOG_LEVEL_ERROR,'Completed we have a valif file name'.$filename);
} // if no file, don't error, but return blank
else {
//$modx->log(modX::LOG_LEVEL_ERROR,'filename is blank -'.$filename);
//$modx->log(modX::LOG_LEVEL_ERROR,'before the $Photo_fields != if-'.$Photo_fields);
//return exsisting photofields value
if ($Photo_fields != '')
{
//$modx->log(modX::LOG_LEVEL_ERROR,'in the $Photo_fields != if-'.$Photo_fields);
//$modx->log(modX::LOG_LEVEL_ERROR,'Completed using already entered Photo_fields-'.$Photo_fields);
$hook->setValue($sf, $Photo_fields);
}
else{
// is the file name empty (no file uploaded) and exsiting photofields empty
$hook->setValue($sf, '');
//$modx->log(modX::LOG_LEVEL_ERROR,'at a last else hook should have a no known value for photofields -'.$Photo_fields);
//$modx->log(modX::LOG_LEVEL_ERROR,'file anem should be blank as well..... -'.$filename);
}
}
}
return $output;
// == Profile Page Snippet
<?php
$output = "";
$id = $modx->getOption('id', $scriptProperties);
if(empty($id)){die();}
$user = $modx->getUser($id);
$profile = $user->getOne('Profile');
$active = $user->get('active');
if($active){
$fullname = $profile->get('fullname');
$extended = $profile->get('extended');
$photo = $extended['Profile_Photos'];
$output = $modx->parseChunk('profile_tpl', array('photo' => $photo,'fullname' => $fullname));
} else {
$output = "User Not Active";
}
return $output;
// == Profile Output Tpl
<img alt="[[+fullname]]" src="[[+photo]]">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment