I needed to add a profile image to existing account records using the user update form that’s part of the Website Membership Plugin. I also wanted to enable replacing that profile image when desired.
I was stumped until I came across this hidden gem provided by Chris way back in 2010.
Note that with a great deal of help from Daniel Louwe, 'ignoreHidden' => true was added to the get records call so that hidden records would not throw an error and I was able to revise the code to accommodate multiple profile images in the same record (My account records were for group therapy practices with more than one therapist and I wanted to keep all therapists on the same record and have a profile image for each)
*** The key is to increment all variables for each new profile image. So $current_user_with_uploads becomes $current_user_with_uploads1, then $current_user_with_uploads2. $uploadFieldName becomes $uploadFieldName1, then $uploadFieldName2 etc.
1) Add an upload field to the accounts table called profile_image and limit the maximum uploads to 2 (this allows an existing profile image to be replaced with a new one). If more than one profile image is required, add additional upload fields named profile_image_1, profile_image_2, etc.
2) Add the following to your existing form table (style as appropriate):
<tr> <td class="your_class" colspan="2"><b>FIRST PROFILE IMAGE</b></td> </tr> <tr> <td colspan="2" class="your_class" valign="top">1 JPG Only - 2 MB max. - Uploading a new image will replace any existing image. Images will be uploaded when the "revalidate or update" button at the bottom of this page is clicked/tapped.</td> <td><?php list($accountRecords,) = getRecords(array('tableName' => 'accounts', 'where' => mysql_escapef('num = ?', $CURRENT_USER['num']), 'allowSearch' => false, 'ignoreHidden' => true)); $current_user_with_uploads = @$accountRecords[0]; ?> <?php if (sizeof(@$current_user_with_uploads['profile_image'])): $upload2 = $current_user_with_uploads['profile_image'][0] ?> <a href="<?php echo $upload2['urlPath'] ?>"> <img src="<?php echo $upload2['thumbUrlPath'] ?>" width="<?php echo $upload2['thumbWidth'] ?>" height="<?php echo $upload2['thumbHeight'] ?>" alt="" /> </a> <?php else: ?> <span class="your_class">No (new) image uploaded</span> <?php endif ?> <span class="your_class">Image To Upload:</span> <input type="file" name="profile_image"></td> </tr>
<tr> <td class="your_class" colspan="2"><b>SECOND PROFILE IMAGE</b></td> </tr> <tr> <td colspan="2" class="your_class" valign="top">1 JPG Only - 2 MB max. - Uploading a new image will replace any existing image. Images will be uploaded when the "revalidate or update" button at the bottom is clicked/tapped.</td> <td><?php list($accountRecords,) = getRecords(array('tableName' => 'accounts', 'where' => mysql_escapef('num = ?', $CURRENT_USER['num']), 'allowSearch' => false, 'ignoreHidden' => true)); $current_user_with_uploads1 = @$accountRecords[0]; // if (!$current_user_with_uploads ) { dieWith404("Record not found!") ; } // code suggested by Daniel 10/2/18 ?> <?php if (sizeof(@$current_user_with_uploads1['profile_image_1'])): $upload1 = $current_user_with_uploads1['profile_image_1'][0] ?> <a href="<?php echo $upload1['urlPath'] ?>"> <img src="<?php echo $upload1['thumbUrlPath'] ?>" width="<?php echo $upload1['thumbWidth'] ?>" height="<?php echo $upload1['thumbHeight'] ?>" alt="" /> </a> <?php else: ?> <span class="your_class">No (new) image uploaded</span> <?php endif ?> <span class="your_class">Image To Upload:</span> <input type="file" name="profile_image_1"></td> </tr>
3) Add ENCTYPE="multipart/form-data" to any other code in your opening <FORM> tag.
4) Add the following just before // update user
// +++++++++++++++++++++++++++++ Begin upload Image 1 +++++++++++++++++++++++++++++++++ $uploadFieldName1 = 'profile_image_practitioner_1'; $uploadInfo1 = @$_FILES[$uploadFieldName1]; if (!empty($uploadInfo1['name']) && !$errorsAndAlerts) { // attempt to save the upload $errors1 = saveUpload('accounts', $uploadFieldName1, $CURRENT_USER['num'], null, $uploadInfo1, $newUploadNums1); // check for errors if ($errors1) { $errorsAndAlerts .= "There was a problem with your upload: $errors1\n"; } else { // if the upload was successful, delete any other uploads associated with that record and field (so users only ever have one Head Shot) global $TABLE_PREFIX; // create query $where = mysql_escapef(" WHERE tableName = 'accounts' AND recordNum = ? AND num != ? AND fieldName = ?", $CURRENT_USER['num'], $newUploadNums1[0], $uploadFieldName1); // remove upload files $query1 = "SELECT (asterisk) FROM `{$TABLE_PREFIX}uploads` $where"; $result1 = mysqli()->query($query1) or die("MySQL Error: ". htmlspecialchars(mysqli()->error) . "\n"); while ($row1 = $result1->fetch_assoc()) { $files1 = array($row1['filePath'], $row1['thumbFilePath'], @$row1['thumbFilePath2'], @$row1['thumbFilePath3'], @$row1['thumbFilePath4']); foreach ($files1 as $filepath1) { if (!$filepath1 || !file_exists($filepath1) || @unlink($filepath1)) { continue; } } } if (is_resource($result1)) { mysql_free_result($result1); } // remove upload records mysqli()->query("DELETE FROM `{$TABLE_PREFIX}uploads` $where") or die("MySQL Error: ". htmlspecialchars(mysql_error()) . "\n"); } } //++++++++++++++++++++++++++++ End upload image 1 ++++++++++++++++++++++++++++
// ++++++++++++++++++++++++++++ Begin upload image 2 +++++++++++++++++++++++++++ $uploadFieldName2 = 'profile_image_practitioner_2'; $uploadInfo2 = @$_FILES[$uploadFieldName2]; if (!empty($uploadInfo2['name']) && !$errorsAndAlerts) { // attempt to save the upload $errors2 = saveUpload('accounts', $uploadFieldName2, $CURRENT_USER['num'], null, $uploadInfo2, $newUploadNums2); // check for errors if ($errors2) { $errorsAndAlerts .= "There was a problem with your upload: $errors2\n"; } else { // if the upload was successful, delete any other uploads associated with that record and field (so users only ever have one Head Shot) global $TABLE_PREFIX; // create query $where = mysql_escapef(" WHERE tableName = 'accounts' AND recordNum = ? AND num != ? AND fieldName = ?", $CURRENT_USER['num'], $newUploadNums2[0], $uploadFieldName2); // remove upload files $query2 = "SELECT (asterisk) FROM `{$TABLE_PREFIX}uploads` $where"; $result2 = mysqli()->query($query2) or die("MySQL Error: ". htmlspecialchars(mysqli()->error) . "\n"); while ($row2 = $result2->fetch_assoc()) { $files2 = array($row2['filePath'], $row2['thumbFilePath'], @$row2['thumbFilePath2'], @$row2['thumbFilePath3'], @$row2['thumbFilePath4']); foreach ($files2 as $filepath2) { if (!$filepath2 || !file_exists($filepath2) || @unlink($filepath2)) { continue; } } } if (is_resource($result2)) { mysql_free_result($result2); } // remove upload records mysqli()->query("DELETE FROM `{$TABLE_PREFIX}uploads` $where") or die("MySQL Error: ". htmlspecialchars(mysql_error()) . "\n"); } } // +++++++++++++++++++++++++++ End upload image 2 ++++++++++++++++++++++++++++++++++
Chris added that more than half of that code is dedicated to removing any other uploads after an upload is successful so that users never have more than one profile image. This is important since I haven't provided any way for users to remove a profile image. They can, of course, replace their current image with another. (This is where the maximum upload limit of 2 comes in. If it was set to allow only 1, then the upload of the replacement image, seen as a second image before the original was removed, would fail.)
|