File Uploader

markhayes 20 Apr, 2012
Hi,

I have a site that I have used the GH custom file uploader to allow customers to upload images. All works fine, and I can display the images in the customer account area using custom code etc.

One thing I can't work out however. I allow the user to edit their form, which can involve upload a new image. When they load the form, the File Upload element does not load the file name that is saved to the DB. (All other user fields are loading fine) and I have called the File Upload element name the same as the DB fields (it is the same form used for the original upload)

Is there something I am missing here?

Mark
GreyHead 20 Apr, 2012
Hi Mark,

I think that the problem is that File Upload actions behave differently and you can't set a value in the same way as you can for a text input. Not the least because you have no idea what the path to the image file on the user's computer is.

I'd get round this by showing the existing image, or a thumb-nail, and adding a file upload that can be used to upload a new image file if needed. You have to watch the code though and make sure that an 'empty' input doesn't over-ride the existing file name (there's been another thread about this).

Bob
markhayes 20 Apr, 2012
Hi Bob,

OK. I am running with your suggestion and my head is wrecked. I have a custom element the code behind it is
<?php
echo $form->data['image2'];
?>


<?php if($form->data['image2']==""): ?>
<img src="/images/misc/no_photo_available.jpg" alt="image" width="100" height="60" />
<?php else: ?>
<img src="<?php echo JURI::Base();?>components/com_content/uploads/{image2}" alt="image" width="100" height="60" />
<?php endif; ?>


But the no photo image is always displayed, even though there defiantly content in image2. I have verified that by looking at DB and slapping a debugger on the form, just before show html. I must be doing something dumb, or approaching this all wrong. Any suggestions?

Mark
markhayes 20 Apr, 2012
OK, the issue is that echo $form->data['image2']; will not output the contents of the DB field, even if the debugger shows it. Do custom element render before event on the unload event? If this is the case, then DB field is not loaded yet when the custom element loads. That would make sense to me, but if this is the case, can I access a DB field value from a custom element on the preview page?
GreyHead 20 Apr, 2012
Hi Mark,


Custom elements are (I think ) rendered when the Show HTML action runs. Try adding this code to a Custom **action**
Code looks OK but try this version without the curly brackets:
<?php
if ( isset($form->data['image2']) && $form->data['image2']  ) {
  $image_url = 'components/com_content/uploads/'.$form->data['image2'];
} else {
  $image_url = 'images/misc/no_photo_available.jpg';
}
$form->data['image_url'] = JURI::base().$image_url;
?>

and leave this in the custom element:
<img src='{image_url}' alt="image" width="100" height="60" />

Bob
markhayes 20 Apr, 2012
Hi,

The custom code looks good (minor change to get rid of closing } at the start. I can see the resulting url in the debugger, the actual result is http://www.roomrenovator.co.uk/components/com_content/uploads/20120420152940_highendglamour.jpg

However the custom element on preview is still acting up. It is not recognizing the {image_url}

The following code

{image_url}
<img src="{image_url}" alt="image" width="100" height="60" />\

outputs the string {image_url} and nothing for the image. (URL = http://www.roomrenovator.co.uk/%7Bimage_url%7D)
GreyHead 20 Apr, 2012
Hi Mark,

Have you got the Curly Replacer turned on in the Show HTML action?

Bob
markhayes 20 Apr, 2012
Just as a quick test, i popped an ordinary text box onto the form can called it image_url. It populates the text box with the correct value. Also, if I put something like {image2} onto the custom element, it outputs the correct db value. It is the {image_url} on the custom element that is not recognized.
GreyHead 21 Apr, 2012
Hi Mark,

There was an extra } in my code where you see {} it should be just {

When this is fixed it appears to work OK. I ran a quick test with this in a Custom Action:
<?php
if ( isset($form->data['image2']) && $form->data['image2']  ) {
  $image_url = 'images/'.$form->data['image2'];
} else {
  $image_url = 'images/joomla_logo_black.jpg';
}
$form->data['image_url'] = JURI::base().$image_url;
?>
and this in a Cusotm element
<img src='{image_url}' alt="image"  />

With the default form URL the joomla_logo_black.jpg shows; if I add &image2=powered_by.png to the URl then the powered-by.png image shows.

Bob
markhayes 23 Apr, 2012
Hi Bob,

My issue was the Curley Brackets replacer. I had spotted the {} in the code. Things are working again now, so I just have to fix up the overwriting file names if no file is selected issue. You mentioned that there was a thread on this somewhere. I will have to track that down.

Thanks for the help.

Mark
markhayes 24 Apr, 2012
Hi Bob,

I can't find that post about not overwriting existing filenames with empty entries. Any suggestions on how to achieve this?

Mark
GreyHead 30 Apr, 2012
Hi Mark,

It's going to be something like this:
<?php
if ( isset($form_data['file_upload']) && !$form_data['file_upload'] ) {
  unset($form_data['file_upload']);
}
?>
where 'file_upload' is the name of the Upload Files element.

Bob
markhayes 06 May, 2012
Hi Bob,

Thanks for that. I did it a different way by not using a DB save action and just wrote an update in PHP with conditional checks on the file name contents. Done now, but your way would have been much quicker!

Thanks anyway

Mark
This topic is locked and no more replies can be posted.