Hi, I had the same error, just put this code into my conrtoller:
Code:
//-----------------------------> ATTACHMENTS FILE SIZE CHECKS <-----------------------------\\
// import joomla clases to manage file system
jimport('joomla.filesystem.folder');
jimport('joomla.filesystem.file');
// get posted attachments
$files = JRequest::getVar('attachments', 'post', 'files', 'array');
// get application reference
$app =& JFactory::getApplication();
// get Lighthouse config
$config =& LighthouseModelConfig::getInstance();
// process attachments
$filesCount = count($files['name']);
for ($i = 0; $i < $filesCount; $i++) {
// check posted attachments
if (strlen(trim($files['name'][$i])) > 0) {
// get maximum size of attachment in bytes
$maxSize = (int)$config->getAttachmentSettings('maximumSize');
// check attachment size
if ($maxSize > 0 && (int)$files['size'][$i] > $maxSize) {
// set error notice
$app->setUserState('warningMsg', JText::_('ATTACHMENT_HUGE').' ( '.JFile::getName($files['name'][$i]).' ) ');
if ($catid != 0)
$this->setRedirect(JRoute::_('index.php?option=com_lighthouse&controller=question&task=create&catid='. $catid .'&'. JUtility::getToken() .'=1', false));
else
$this->setRedirect(JRoute::_('index.php?option=com_lighthouse&controller=question&task=create&zonid='. $zonid .'&'. JUtility::getToken() .'=1', false));
return;
}
}
else {
unset($files['name'][$i]);
unset($files['type'][$i]);
unset($files['tmp_name'][$i]);
unset($files['error'][$i]);
unset($files['size'][$i]);
}
}
$app->setUserState('warningMsg', '');
//-----------------------------> ATTACHMENTS FILE SIZE CHECKS <-----------------------------\\
// get Ticket model
$modelTicket =& $this->getModel('Ticket');
$post = JRequest::get('post');
// let the model save all necessary data
if ($msgid = $modelTicket->store($post, $files)) {
...
The idea is to clear the garbage in the $files (a bunch of unsets) even if nothing was uploaded. And then in the model I upload files:
Code:
function store($post, $files)
{
// get Ticket table
$tableTicket =& $this->getTable();
// prepare grouping
$group = $this->_db->nameQuote('catid') .' = '. intval($post['catid']);
// store ticket
if (!$this->save($post, true, $tableTicket, $group))
return false;
// get Message table
$tableMessage =& $this->getTable('Message');
// specify some post values
$now = JFactory::getDate();
$post['create_date'] = $now->toMySQL();
$post['tickid'] = $tableTicket->tickid;
// prepare grouping
$group = $this->_db->nameQuote('tickid') .' = '. intval($tableTicket->tickid);
// store message
if (!$this->save($post, true, $tableMessage, $group))
return false;
// get Lighthouse config
$config =& LighthouseModelConfig::getInstance();
// process files
if ($config->getAttachmentSettings('isUploadsActivated')) {
for ($i = 0; $i < $config->getAttachmentSettings('allowedNumberPossibleFileUploads'); $i++) {
if (!empty($files['name'][$i])) {
// then create assigned attachment
$attachment = new StdClass;
$attachment->attid = 0;
$attachment->name = JFile::makeSafe(JFile::stripExt($files['name'][$i]));
$attachment->extension = JFile::getExt($files['name'][$i]);
// create folder for attachment
if (!JFolder::create(JPATH_COMPONENT_SITE.DS.'attachments'.DS.'message '.$tableMessage->msgid, 0775)) {
// handle failed folder creation
$this->setError(get_class($this).'::store attachment failed - cannot create folder');
return false;
}
else
$attachment->folder = JPATH_COMPONENT_SITE.DS.'attachments'.DS.'message '.$tableMessage->msgid;
$attachment->sizekb = $files['size'][$i] / 1024;
$attachment->msgid = $tableMessage->msgid;
// and store it
if(!$this->_db->insertObject('#__lighthouse_attachment', $attachment, 'attid')) {
$this->setError(get_class( $this ).'::store attachment failed - '.$this->_db->getErrorMsg());
return false;
}
// specify destination folder
$filePath = JPath::clean($attachment->folder.DS.$attachment->name.'.'.$attachment->extension);
// save file in the folder
if (!JFile::upload($files['tmp_name'][$i], $filePath)) {
// handle failed upload
$this->setError(get_class( $this ).'::save attachment failed - cannot upload file');
return false;
}
}
}
}
return $tableMessage->msgid;
}
When I not only upload files, but also keep tracks in the DB, what is not recommended.
This code checked with several extensions. Pay attention to the
unset instructions. I think it is your solution
jlighthouse
_________________
Currently developing one of the few and free Joomla ticket system, called Lighthouse -
http://extensions.joomla.org/extensions ... -desk/8098