Forums

Trouble using $attachments array in OnSubmit code

benc 21 Apr, 2009
I am using chronoforms 3.1 RC4.11.

I am trying to use a form to upload files and then move the files to a new location based on the user that is uploading. I found the other posts that give the solution to this, but I can't get it to work. There seems to be a problem with using the $attachments array in the OnSubmit box. I inserted a debug message that should print out the path stored in the array, but nothing is shown. My files upload correctly to the original path, but they are not moved to the new folder. The new folders are created correctly and permissions are set correctly.

Here is what I have in the OnSubmit after email box.


<?php
// set field names for your form
$field_name = 'file_1';

$user =& JFactory::getUser();

// set the path to save to
$target_folder = JPATH_SITE.DS.'images'.DS.'uploads'.DS.$user->username.DS.headshot;

// create the target folder if it doesn't exist
if ( !JFolder::exists($target_folder) ) {
    JFolder::create($target_folder);
}

// get the file name
$filename = basename($attachments[$field_name]);
$MyForm->addDebugMsg($attachments[$field_name].' mydebugmsg');

// check that the file exists and move it
if ( JFile::exists($attachments[$field_name]) ) {
  JFile::move($attachments[$field_name], $target_folder.DS.$filename);
}
?>


Do you have an idea as to what I might be doing wrong? Thanks.
Max_admin 22 Apr, 2009
Hi Benc,

instead of $attachments please use : $MyForm->attachments

Regards,
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
benc 22 Apr, 2009
Hi Max,

Thanks for the reply. I tried replacing $attachments with $MyForm->attachments but nothing changed. The files still upload to the original folder but don't get moved. The debug message I added in the code (shown above) still just prints out 'mydebugmsg' without the path that is supposed to be in $attachments.

Thanks,
Ben
Max_admin 22 Apr, 2009
this code is in the Onsubmit after email box ?

did you replace all the $attachments variables ?

at the begining of the code please add this line:


$MyForm =& CFChronoForm::getInstance('form_name'); // replace form_name with real form name with no spaces
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
benc 22 Apr, 2009
Yes, this code is in the OnSubmit after email box. I did replace all instances of $attachments. Below is the modified code.


$MyForm =& CFChronoForm::getInstance('myform');
...
// get the file name
$filename = basename($MyForm->attachments[$field_name]);
//$MyForm->addDebugMsg($MyForm->attachments[$field_name].' mydebugmsg');

// check that the file exists and move it
if ( JFile::exists($MyForm->attachments[$field_name]) ) {
  JFile::move($MyForm->attachments[$field_name], $target_folder.DS.$filename);
}
Max_admin 24 Apr, 2009
Sorry, you should have :

$MyUploads =& CFUploads::getInstance($MyForm->formrow->id);


then you can use : $MyUploads->attachments not $MyForm->attachments

Regards
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
benc 24 Apr, 2009
Thanks Max! It works now!
This topic is locked and no more replies can be posted.