Main
Index
Search
Posts
Who's
Online
Log
In

Home: Products: CMS Builder: Plugins & Add-ons:
Website Membership Plugin -- Uploading Image(s)

 

 


yusuke
User

Sep 12, 2010, 7:34 AM

Post #1 of 18 (7028 views)
Shortcut
Website Membership Plugin -- Uploading Image(s) Can't Post

Hi,

I'd like members (using Website Membership Plugin) to upload their profile image(s), and of course display them.

Will you please tell me how to do this?

I am looking for the similar function as this one...

http://www.interactivetools.com/iforum/Products_C2/CMS_Builder%3A_Plugins_%26_Add-ons_F40/gforum.cgi?post=78619;search_string=membership%20upload;t=search_engine#78619


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>:


Code
   <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":


Code
    // 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)


yusuke
User

Sep 13, 2010, 12:44 AM

Post #3 of 18 (7012 views)
Shortcut
Re: [chris] Website Membership Plugin -- Uploading Image(s) [In reply to] Can't Post

Hi Chris.

This is GREAT! Wow!
Everything is working beautifully.

Thank you so much for your very easy-to-understand instruction, and of course the wonderful code.

I know you would've already done it if it was possible but would it be possible to upload multiple images/user?


Chris
Staff / Moderator


Sep 13, 2010, 10:38 AM

Post #4 of 18 (6961 views)
Shortcut
Re: [yusuke] Website Membership Plugin -- Uploading Image(s) [In reply to] Can't Post

Hi yusuke,

It's certainly possible, just more complicated.

Firstly, you'd want to remove the "remove upload files" block of code above, since that prevents users from uploading more than one image for your field.

Next you'd need to provide a way for users to delete images. The UI is more tricky than actually removing an image. You'd probably want to use JavaScript to fire a request to remove the image without reloading the page (and discarding the user's other changes to the form) and also remove the image element from the page. Another way to do it would be to have checkboxes beside images to delete them.

Once you've decided which image to delete, (preferably based on the upload record's num,) you can delete an upload like this:


Code
function removeUpload($uploadNum) { 
$where = mysql_escapef('num = ?', $uploadNum);

// remove upload files
$row = mysql_query_fetch_row_assoc("SELECT * FROM `{$TABLE_PREFIX}uploads` $where");
$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);
}

// remove upload record
mysql_query("DELETE FROM `{$TABLE_PREFIX}uploads` $where") or die("MySQL Error: ". htmlspecialchars(mysql_error()) . "\n");
}

Chris


(This post was edited by chris on Sep 13, 2010, 10:39 AM)


yusuke
User

Sep 13, 2010, 8:49 PM

Post #5 of 18 (6915 views)
Shortcut
Re: [chris] Website Membership Plugin -- Uploading Image(s) [In reply to] Can't Post

Hi Chris,

Thank you so much for your reply!

Even though I understand what you are describing, I have no idea how to implement them, especially the UI part.

How should I move forward on this matter?
If it requires custom programming, will you please let me know so I can ask my client?

Thanx!


Chris
Staff / Moderator


Sep 13, 2010, 9:13 PM

Post #6 of 18 (6913 views)
Shortcut
Re: [yusuke] Website Membership Plugin -- Uploading Image(s) [In reply to] Can't Post

Hi yusuke,

Yes, this is definitely something that our consulting department could help you with. If this is something you're interested in doing, please send an email to consulting@interactivetools.com going over the functionality you would want and we can get you a quote on what this would cost.
Chris


yusuke
User

Sep 13, 2010, 9:43 PM

Post #7 of 18 (6911 views)
Shortcut
Re: [chris] Website Membership Plugin -- Uploading Image(s) [In reply to] Can't Post

Hi Chris,

I will ask my client about it asap.

Thanx!


Thomas
User

Apr 10, 2011, 7:01 AM

Post #8 of 18 (5756 views)
Shortcut
Re: [Chris] Website Membership Plugin -- Uploading Image(s) [In reply to] Can't Post

Can a modified version of this be used in the signup form?


(This post was edited by Thomas on Apr 10, 2011, 7:01 AM)


Jason
Staff / Moderator


Apr 11, 2011, 10:13 AM

Post #9 of 18 (5743 views)
Shortcut
Re: [Thomas] Website Membership Plugin -- Uploading Image(s) [In reply to] Can't Post

Hi,

Yes, you could do this in the signup form. Give it a try and let us know if you run into any issues.

Thanks
---------------------------------------------------
Jason Sauchuk - Programmer 
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/ 


Maurice
User

Apr 12, 2011, 5:45 AM

Post #10 of 18 (5726 views)
Shortcut
Re: [yusuke] Website Membership Plugin -- Uploading Image(s) [In reply to] Can't Post

This code is great and works wel,

Could you also create a code or plugin with the membership function to upload multiple images and few the current images just like the the back-end of CMSB,

Ithink many people would be happy and great full.

Greetz Maurice
-------------------------------------------
www.intraserve.nl

Soon! cmsBy.intraserve


Hybrid
User

Jun 24, 2011, 12:26 PM

Post #11 of 18 (5344 views)
Shortcut
Re: [Chris] Website Membership Plugin -- Uploading Image(s) [In reply to] Can't Post

I followed your example. It works to upload my image but there is an issue. When I click update without a new image to upload, it removes my old image. Are other people getting this?


gkornbluth
Veteran

Dec 12, 2011, 1:20 PM

Post #12 of 18 (4101 views)
Shortcut
Re: [Jason] Website Membership Plugin -- Uploading Image(s) [In reply to] Can't Post

Hi all,

I’ve inherited another designer's work and I’m having a (huge) bit of difficulty implementing this last bit of code.

I need to upload an image that’s already being saved to a folder by the existing code into to an upload field in a CMSB database record.

There are 2 active files involved in this process, (purchase.php (the form) and ordernow.php (the processing file)). There’s also an error checking file that is not involved at this point

The purchase.php (file) contains the form that gets submitted, which already contains an image upload field.

After error checking and some price calculations, the data is passed to the ordernow.php file (attached) that uploads the image to directory called “photo” and attaches the image to an email message that gets sent along with all of the form’s other information.

I’ve been able to use the series of existing variables in the ordernow.php file (attached) to create and populate the text based fields in a CMS database record in a customer_uploads table.

The problem comes in when I try to get the existing image inserted into the CMS record also.

There will only ever be one image and once uploaded the client will never have to change or delete it.

Here's some of the existing code in ordernow.php that I have to work with:


Code
 $name_of_uploaded_file =basename($_FILES['photo']['name']); 
//get the file extension of the file
$type_of_uploaded_file =substr($name_of_uploaded_file,strrpos($name_of_uploaded_file, '.') + 1);
//get the size of the file
$size_of_uploaded_file =$_FILES["photo"]["size"]/1024;//size in KBs

//copy the temp. uploaded file to attachdoc folder
$upload_folder='photo/';
$path_of_uploaded_file = $upload_folder . $name_of_uploaded_file;

//echo 'path_of_uploaded_file'.$path_of_uploaded_file;

$tmp_path = $_FILES["photo"]["tmp_name"];

if(is_uploaded_file($tmp_path))
{
copy($tmp_path,$path_of_uploaded_file);
if(!copy($tmp_path,$path_of_uploaded_file))
{
$errors .= '\n error while copying the uploaded file';
}
}


I thought it would be fairly straight forward but I’ve tried butchering and implementing the code above and I’m getting nowhere.

I could really use some help.

Thanks,

Jerry Kornbluth
The first CMS Builder reference book is now available on-line!
http://www.thecmsbcookbook.com
Attachments: purchase.php (27.1 KB)
  ordernow.php (16.0 KB)


Jason
Staff / Moderator


Dec 13, 2011, 10:34 AM

Post #13 of 18 (4056 views)
Shortcut
Re: [gkornbluth] Website Membership Plugin -- Uploading Image(s) [In reply to] Can't Post

Hi Jerry,

Is the image being successfully uploaded to a file on the server? If so, you can then get it into CMSB using the saveUploadFromFilepath() function.

In this example, I'm assuming that the name of your upload field is "image":


Code
      $userNum = mysql_insert_id(); 

saveUploadFromFilepath('customer_uploads', 'image', $userNum, '123456789' , $path_of_uploaded_file);



Hope this helps get you started.
---------------------------------------------------
Jason Sauchuk - Programmer 
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/ 


gkornbluth
Veteran

Dec 13, 2011, 4:34 PM

Post #14 of 18 (4047 views)
Shortcut
Re: [Jason] Website Membership Plugin -- Uploading Image(s) [In reply to] Can't Post

Thanks Jason,

Again a perfect (and elegant) solution. (and another recipe for the cookbook)

Jerry Kornbluth
The first CMS Builder reference book is now available on-line!
http://www.thecmsbcookbook.com


gkornbluth
Veteran

Dec 16, 2011, 5:03 AM

Post #15 of 18 (3999 views)
Shortcut
Re: [Jason] Website Membership Plugin -- Uploading Image(s) [In reply to] Can't Post

Hi Jason,

Seems I fix one thing and I break another.

In post 91302 you helped me to update a record using a where clause to filter which record would be updated.

This works fine except for inserting an image into the record when updating. (I thought the same code you suggested here would also work for a record update) BTW: The new image file does get saved to the server.

I'm using this to upload an image where none existed before. I guess another question will be, will this replace an image if one exists?

The code I'm using to update the record is below. (complete file is attached)

Hope it's an easy fix.

Thanks,

Jerry Kornbluth

Code
/load records 
list($customer_uploadsRecords, $customer_uploadsMetaData) = getRecords(array(
'tableName' => 'customer_uploads',
));

mysql_query("UPDATE `{$TABLE_PREFIX}customer_uploads` SET

email = '".$email."',
first_name = '".$first_name."',
last_name = '".$last_name1."',
street_address = '".$street_address."',
city = '".$city."',
state = '".$state."',
zip = '".$zip."',

sales_id = '".$sales_id."',
createdDate = NOW(),
updatedDate = NOW(),
createdByUserNum = '0',
updatedByUserNum = '0'
WHERE activation_code = '".mysql_escape($activation_code)."'")
or die("MySQL Error updating Record:<br/>\n". htmlspecialchars(mysql_error()) . "\n");
$userNum = mysql_insert_id();
@saveUploadFromFilepath('customer_uploads', 'uploads', $userNum, '123456789' , $path_of_uploaded_file);
$errors='Thank You<br /><br />Your Portrait Order Has Been Successfully Placed.';
}

?>

The first CMS Builder reference book is now available on-line!
http://www.thecmsbcookbook.com
Attachments: ordernow3.php (11.7 KB)


Jason
Staff / Moderator


Dec 16, 2011, 8:06 AM

Post #16 of 18 (3993 views)
Shortcut
Re: [gkornbluth] Website Membership Plugin -- Uploading Image(s) [In reply to] Can't Post

Hi Jerry,

So you're trying to save an upload to all the records that just got updated. Is that right?

I think the problem will have to do with $userNum. Since your update query could be updating multiple records, mysql_insert_id() won't be sufficient.

What you could try is to retrieve all the records that were just updated, and perform the saveUploadFromFilePath() call on each record.

example:


Code
  $customerUploadRecords = mysql_select("customer_upload", "activation_code = '".mysql_escape($activation_code)."'"); 

foreach ($customerUploadRecords as $record) {
@saveUploadFromFilepath('customer_uploads', 'uploads', $record['num'], '123456789' , $path_of_uploaded_file);
}


Hope this helps
---------------------------------------------------
Jason Sauchuk - Programmer 
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/ 


gkornbluth
Veteran

Dec 16, 2011, 10:10 AM

Post #17 of 18 (3988 views)
Shortcut
Re: [Jason] Website Membership Plugin -- Uploading Image(s) [In reply to] Can't Post

Thanks again Jason,

I only needed it for the single image in the single record. but I didn't change the code you suggested. and it worked perfectly.
_______

I hope you don't mind my asking one more small question.

I've managed to implement a check for an existing image in the html returned to the customer, but I'm having trouble checking the database and not allowing a second upload with the code you suggested. (right now it keeps adding images)

Everything I've tried made quite a mess.

Any suggestions?

Jerry Kornbluth

The code I used: entire file attached:

Code
<?php if (!$customer_uploadsRecords): ?> 
<span class="body-text-11">We're sorry.<br /><br />Activation Code "<?php echo @$activation_code; ?>" wasn't found in our system.<br /><br />Please check the number you entered, and then click <a href="upload.php">HERE</a> to try again, or <a href="contact.php">HERE</a> to contact customer support. <br/><br/></span><?php else: ?>
<?php foreach ($customer_uploadsRecords as $record): ?>
<?php if (sizeof($record['uploads']) >= 1): ?><span class="body-text-bold-11">There's already an image uploaded for the order with Activation Code, "<?php echo @$activation_code; ?>".<br /><br />Sorry, you can't upload a second image.<br /><br />Click <a href="purchase.php">HERE</a> to place a new order, or <a href="contact.php">HERE</a> to contact customer support.</span>


<?php else: ?>
<span class="body-text-bold-11"><?php echo @$errors;?></span><br />
<span class="body-text-11"><?php echo $common_informationRecord['image_upload_confirmation_page_text'] ?></span><br />
<span class="body-text-11"><?php if (@$name_of_uploaded_file): ?><br />Uploaded Image File Name: "<?php echo @$name_of_uploaded_file; ?>"<?php else: ?><br />No Image was uploaded with this order.<?php endif ?>

</span></p>
<span class="body-text-11">Activation Code: "<?php echo @$activation_code; ?>"</span> <?php endif ?> <?php endforeach; ?><?php endif ?>

The first CMS Builder reference book is now available on-line!
http://www.thecmsbcookbook.com
Attachments: ordernow3.php (12.7 KB)


Jason
Staff / Moderator


Dec 19, 2011, 7:42 AM

Post #18 of 18 (3861 views)
Shortcut
Re: [gkornbluth] Website Membership Plugin -- Uploading Image(s) [In reply to] Can't Post

Hi Jerry,

In the foreach loop, you can query the uploads table to check to see if there are any uploaded records for the current record.

Try this:


Code
foreach ($customerUploadsRecords as $record) { 
if (mysql_count('uploads', "tableName = 'customer_uploads' AND fieldName = 'uploads' AND recordNum = '".mysql_escape($record['num'])."'")) {
$errors .= "You have already uploaded an image for this record! <br/>\n";
}
else{
@saveUploadFromFilepath('customer_uploads', 'uploads', $record['num'], '123456789' , $path_of_uploaded_file);
}
}
$errors .= 'Thank You<br /><br />Your Portrait Order Has Been Successfully Updated.';


You'll notice that I also changed the last line with $errors to append to the string, instead of assigning a value to it. This is so any errors don't get overwritten.

Hope this helps
---------------------------------------------------
Jason Sauchuk - Programmer 
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/