Page 1 of 1
[SOLVED]Turn Off Joomla! Warning
Posted: Fri Feb 26, 2010 4:22 pm
by knucklesmcfly
Hey all...I have a script in MVC form that handles a form post with an optional file upload...My problem is that when the user does not input a file to upload, it's optional, I get a Joomla! warning on the redirected page that says, "Warning: Failed to move file." I know it's a Joomla! warning because it has the warning icon.
How do I turn that off?
Thanks all...
Re: Turn Off Joomla! Warning
Posted: Fri Feb 26, 2010 4:27 pm
by Marcus Stafford
Go to Global Configuration and click Server.
Set 'Error Reporting' to None.
Re: Turn Off Joomla! Warning
Posted: Fri Feb 26, 2010 5:08 pm
by knucklesmcfly
Tried that. It didn't work.
Re: Turn Off Joomla! Warning
Posted: Fri Feb 26, 2010 6:13 pm
by dhuelsmann
Why is it trying to upload the file if the user didn't input anything? Sounds like you need to adjust your code so that it doesn't try to upload if there was no input
Re: Turn Off Joomla! Warning
Posted: Fri Feb 26, 2010 7:32 pm
by knucklesmcfly
Here's the code:
Code: Select all
if ($image)
{
jimport('joomla.filesystem.file');
$imagename = JFile::makeSafe($image['name']);
$src = $image['tmp_name'];
$dest = JPATH_COMPONENT . DS . "uploads" . DS . $imagename;
if ( strtolower(JFile::getExt($imagename) ) == 'jpg' || 'png')
{
if ( JFile::upload($src, $dest) ) {
$filemsg = 'Your image has been uploaded';
} else {
$filemsg = 'There was an error uploading your image';
}
} else {
$filemsg = 'Image is not proper file extension';
}
}
Re: Turn Off Joomla! Warning
Posted: Fri Feb 26, 2010 7:35 pm
by dhuelsmann
Where are you making sure that $image is set to null before input?
Re: Turn Off Joomla! Warning
Posted: Sat Feb 27, 2010 10:58 am
by knucklesmcfly
dhuelsmann wrote:Where are you making sure that $image is set to null before input?
In the controller:
Code: Select all
$image = JRequest::getVar('image', null, 'files');
Re: Turn Off Joomla! Warning
Posted: Sat Feb 27, 2010 11:15 am
by knucklesmcfly
When I echo out $image it gives me the value of Array.
Re: Turn Off Joomla! Warning
Posted: Sat Feb 27, 2010 3:39 pm
by jlighthouse
Hi, I had the same error, just put this code into my conrtoller:
Code: Select all
//-----------------------------> 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: Select all
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
Re: Turn Off Joomla! Warning
Posted: Mon Mar 01, 2010 3:16 pm
by knucklesmcfly
This seems like an uncanny way to accomplish this. But I'm going to give it a try.
Re: Turn Off Joomla! Warning
Posted: Tue Mar 02, 2010 9:56 am
by dam-man
Do you one or more attachments at one save?
You could simply check if the temp name is filled, if so process upload else don't upload. Looks much easier to me..
Code: Select all
if ($image['tmp_name'] != '')
{
jimport('joomla.filesystem.file');
$imagename = JFile::makeSafe($image['name']);
$src = $image['tmp_name'];
$dest = JPATH_COMPONENT . DS . "uploads" . DS . $imagename;
if ( strtolower(JFile::getExt($imagename) ) == 'jpg' || 'png')
{
if ( JFile::upload($src, $dest) ) {
$filemsg = 'Your image has been uploaded';
} else {
$filemsg = 'There was an error uploading your image';
}
} else {
$filemsg = 'Image is not proper file extension';
}
}
Re: Turn Off Joomla! Warning
Posted: Tue Mar 02, 2010 1:59 pm
by knucklesmcfly
dam-man you know what? It's funny because this was actually the solution I wound up using. I use a check to see if $file['name'] is not null.
Thanks much for the reply.
Re: Turn Off Joomla! Warning
Posted: Wed Mar 03, 2010 8:18 am
by dam-man
knucklesmcfly wrote:dam-man you know what? It's funny because this was actually the solution I wound up using. I use a check to see if $file['name'] is not null.
Thanks much for the reply.
You're welcome. And have fun with your script!