The Joomla! Forum ™



Forum rules


Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 13 posts ] 
Author Message
PostPosted: Fri Feb 26, 2010 4:22 pm 
User avatar
Joomla! Enthusiast
Joomla! Enthusiast

Joined: Thu Jan 15, 2009 4:28 am
Posts: 198
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... :pop


Last edited by knucklesmcfly on Tue Mar 02, 2010 1:59 pm, edited 1 time in total.

Top
 Profile  
 
PostPosted: Fri Feb 26, 2010 4:27 pm 
User avatar
Joomla! Explorer
Joomla! Explorer

Joined: Tue Apr 17, 2007 8:50 am
Posts: 289
Location: Norwich, England
Go to Global Configuration and click Server.

Set 'Error Reporting' to None.

_________________
Joomla! Managed Hosting, Web Design, Hack Repairs and Consulting ~ https://www.wintercorn.com


Top
 Profile  
 
PostPosted: Fri Feb 26, 2010 5:08 pm 
User avatar
Joomla! Enthusiast
Joomla! Enthusiast

Joined: Thu Jan 15, 2009 4:28 am
Posts: 198
Tried that. It didn't work.


Top
 Profile  
 
PostPosted: Fri Feb 26, 2010 6:13 pm 
User avatar
Joomla! Master
Joomla! Master

Joined: Sun Oct 02, 2005 12:50 am
Posts: 18757
Location: Omaha, NE
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

_________________
Regards, Dave
http://www.kiwaniswest.org


Top
 Profile  
 
PostPosted: Fri Feb 26, 2010 7:32 pm 
User avatar
Joomla! Enthusiast
Joomla! Enthusiast

Joined: Thu Jan 15, 2009 4:28 am
Posts: 198
Here's the code:

Code:
      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';
            }   
         
      }


Top
 Profile  
 
PostPosted: Fri Feb 26, 2010 7:35 pm 
User avatar
Joomla! Master
Joomla! Master

Joined: Sun Oct 02, 2005 12:50 am
Posts: 18757
Location: Omaha, NE
Where are you making sure that $image is set to null before input?

_________________
Regards, Dave
http://www.kiwaniswest.org


Top
 Profile  
 
PostPosted: Sat Feb 27, 2010 10:58 am 
User avatar
Joomla! Enthusiast
Joomla! Enthusiast

Joined: Thu Jan 15, 2009 4:28 am
Posts: 198
dhuelsmann wrote:
Where are you making sure that $image is set to null before input?


In the controller:
Code:
$image = JRequest::getVar('image', null, 'files');


Top
 Profile  
 
PostPosted: Sat Feb 27, 2010 11:15 am 
User avatar
Joomla! Enthusiast
Joomla! Enthusiast

Joined: Thu Jan 15, 2009 4:28 am
Posts: 198
When I echo out $image it gives me the value of Array.


Top
 Profile  
 
PostPosted: Sat Feb 27, 2010 3:39 pm 
User avatar
Joomla! Intern
Joomla! Intern

Joined: Sun Feb 14, 2010 2:07 pm
Posts: 78
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


Top
 Profile  
 
PostPosted: Mon Mar 01, 2010 3:16 pm 
User avatar
Joomla! Enthusiast
Joomla! Enthusiast

Joined: Thu Jan 15, 2009 4:28 am
Posts: 198
This seems like an uncanny way to accomplish this. But I'm going to give it a try.


Top
 Profile  
 
PostPosted: Tue Mar 02, 2010 9:56 am 
User avatar
Joomla! Exemplar
Joomla! Exemplar

Joined: Fri Sep 09, 2005 2:13 pm
Posts: 7962
Location: The Netherlands
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:
      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';
            }   
         
      }

_________________
Robert Dam - Joomla Forum Moderator
Dutch Boards | Joomla Coding Boards | English Support Boards


Top
 Profile  
 
PostPosted: Tue Mar 02, 2010 1:59 pm 
User avatar
Joomla! Enthusiast
Joomla! Enthusiast

Joined: Thu Jan 15, 2009 4:28 am
Posts: 198
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.


Top
 Profile  
 
PostPosted: Wed Mar 03, 2010 8:18 am 
User avatar
Joomla! Exemplar
Joomla! Exemplar

Joined: Fri Sep 09, 2005 2:13 pm
Posts: 7962
Location: The Netherlands
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!

_________________
Robert Dam - Joomla Forum Moderator
Dutch Boards | Joomla Coding Boards | English Support Boards


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 13 posts ] 



Who is online

Users browsing this forum: No registered users and 13 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group