
Chris
Staff
/ Moderator

Sep 12, 2010, 3:20 PM
Post #2 of 18
(7027 views)
Shortcut
|
|
Re: [yusuke] Website Membership Plugin -- Uploading Image(s)
[In reply to]
|
Can't Post
|
|
Hi yusuke, This isn't currently very simple to do, but I've written some code that should get you started. Before I even started, I added an Upload field to the accounts table called Profile Image. You can add this code to your profile editing page. I added it to the sample_profile.php page that comes with the Website Membership plugin to try it out and make sure it works. First, I added this row to the <table>:
<tr> <td valign="top">Profile Image</td> <td> <?php list(list($current_user_with_uploads),) = getRecords(array('tableName' => 'accounts', 'where' => mysql_escapef('num = ?', $CURRENT_USER['num']), 'allowSearch' => false)); ?> <?php if (sizeof(@$current_user_with_uploads['profile_image'])): $upload = $current_user_with_uploads['profile_image'][0] ?> <a href="<?php echo $upload['urlPath'] ?>"> <img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" alt="" /><br/> </a> <?php else: ?> No image uploaded<br /> <?php endif ?> Upload: <input type="file" name="profile_image"> </td> </tr> Note that I needed to call getRecords() because the $CURRENT_USER array doesn't contain uploads. Then I added this code immediately above "// update user":
// upload 'profile_image' if supplied $uploadFieldName = 'profile_image'; $uploadInfo = @$_FILES[$uploadFieldName]; if ($uploadInfo && !$errorsAndAlerts) { /// attempt to save the upload $errors = saveUpload('accounts', $uploadFieldName, $CURRENT_USER['num'], null, $uploadInfo, $newUploadNums); /// check for errors if ($errors) { $errorsAndAlerts .= "There was a problem with your upload: $errors<br/>\n"; } else { /// if the upload was successful, delete any other uploads associated with that record and field (so users only ever have 1 profile image) global $TABLE_PREFIX; // create query $where = mysql_escapef(" WHERE tableName = 'accounts' AND recordNum = ? AND num != ?", $CURRENT_USER['num'], $newUploadNums[0]); // remove upload files $query = "SELECT * FROM `{$TABLE_PREFIX}uploads` $where"; $result = mysql_query($query) or die("MySQL Error: ". htmlspecialchars(mysql_error()) . "\n"); while ($row = mysql_fetch_assoc($result)) { $files = array($row['filePath'], $row['thumbFilePath'], @$row['thumbFilePath2'], @$row['thumbFilePath3'], @$row['thumbFilePath4']); foreach ($files as $filepath) { if (!$filepath || !file_exists($filepath) || @unlink($filepath)) { continue; } //$error = "Unable to remove file '" .htmlspecialchars($filepath). "'\n\n"; //$error .= "Please ask your server administrator to check permissions on that file and directory.\n\n"; //$error .= "The PHP error message was: $php_errormsg\n"; //die($error); } } if (is_resource($result)) { mysql_free_result($result); } // remove upload records mysql_query("DELETE FROM `{$TABLE_PREFIX}uploads` $where") or die("MySQL Error: ". htmlspecialchars(mysql_error()) . "\n"); } } 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.) The only other thing left to do is to make sure that your <FORM> has ENCTYPE="multipart/form-data" so that web browsers will allow uploads. I hope this helps get you started! Please let me know if you have any questions. Chris
(This post was edited by chris on Sep 12, 2010, 3:23 PM)
|