Talk

How to use the filesystem package: Difference between revisions

From Joomla! Documentation

New page: The example shows a terrible coding practice: nesting ifs if ( strtolower(JFile::getExt($filename) ) == 'jpg') { if ( JFile::upload($src, $dest) ) { //Redirect to a pag...
 
Use of JInput: new section
 
Line 23: Line 23:


(Ideally we'd put this in a function and return after each if, but KISS :) )
(Ideally we'd put this in a function and return after each if, but KISS :) )
== Use of JInput ==
According to https://docs.joomla.org/Retrieving_request_data_using_JInput, JFactory::getApplication()->input->get('file_upload'); should be JFactory::getApplication()->input->files->get('file_upload');
Should the doc be changed for all versions of Joomla (e.g. the doc is incorrect), or is this change only applicable to 3.x?

Latest revision as of 13:59, 15 December 2017

The example shows a terrible coding practice: nesting ifs


  if ( strtolower(JFile::getExt($filename) ) == 'jpg') {
     if ( JFile::upload($src, $dest) ) {
        //Redirect to a page of your choice
     } else {
        //Redirect and throw an error message
     }
  } else {
     //Redirect and notify user file is not right extension
  }


This sort of thing creates huge chains of if's that no knows how to look at. I think a better sample code would be:

  if ( strtolower(JFile::getExt($filename) ) != 'jpg')
     //Redirect and notify user file is not right extension
  else if ( !JFile::upload($src, $dest) )
     //Redirect and throw an error message
  else
     //Redirect to a page of your choice

(Ideally we'd put this in a function and return after each if, but KISS :) )

Use of JInput

According to https://docs.joomla.org/Retrieving_request_data_using_JInput, JFactory::getApplication()->input->get('file_upload'); should be JFactory::getApplication()->input->files->get('file_upload');

Should the doc be changed for all versions of Joomla (e.g. the doc is incorrect), or is this change only applicable to 3.x?