Website Membership Plugin -- Uploading Image(s)

By racersdivision - June 24, 2011

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?

Re: [Jason] Website Membership Plugin -- Uploading Image(s)

By gkornbluth - December 12, 2011

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:

$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!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Attachments:

purchase.php 28K

ordernow.php 16K

Re: [gkornbluth] Website Membership Plugin -- Uploading Image(s)

By Jason - December 13, 2011

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

$userNum = mysql_insert_id();

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


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

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

Re: [Jason] Website Membership Plugin -- Uploading Image(s)

By gkornbluth - December 13, 2011

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!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Re: [Jason] Website Membership Plugin -- Uploading Image(s)

By gkornbluth - December 16, 2011

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
/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!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Attachments:

ordernow3_001.php 12K

Re: [gkornbluth] Website Membership Plugin -- Uploading Image(s)

By Jason - December 16, 2011

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:

$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 - Project Manager
interactivetools.com

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

Re: [Jason] Website Membership Plugin -- Uploading Image(s)

By gkornbluth - December 16, 2011

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







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Attachments:

ordernow3_002.php 13K

Re: [gkornbluth] Website Membership Plugin -- Uploading Image(s)

By Jason - December 19, 2011

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:

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 - Project Manager
interactivetools.com

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