1

I have multiple custom file inputs in my module , id like to save all this inputs in my database table ("Custom table , not the Joomla tables") , below is my code , which saves only 1 input , there are 4 inputs :

helper.php

class moduploaddocumentsHelper
{
    /**
     * Retrieves the hello message
     *
     * @param   array  $params An object containing the module parameters
     *
     * @access public
     */
    public static function getdata()
    {
       //Retrieve file details from uploaded file, sent from upload form
        $file = JRequest::getVar('highestacademic', null, 'files', 'array');

        //Import filesystem libraries. Perhaps not necessary, but does not hurt
        jimport('joomla.filesystem.file');

        //Clean up filename to get rid of strange characters like spaces etc
        $filename =  uniqid().JFile::makeSafe($file['name']);
        //Set up the source and destination of the file
        $src = $file['tmp_name'];
        $dest = JPATH_BASE . DS . "media" . DS ."documents" . DS . $filename;


        // $name=$post['highestacademic']['name'] ;
        //First check if the file has the right extension, we need jpg only
        if ( strtolower(JFile::getExt($filename) ) == 'jpg' || strtolower(JFile::getExt($filename) ) == 'png') {
           if ( JFile::upload($src, $dest) ) {
             $user = JFactory::getUser();
             $useid = $user->id ;
             // Obtain a database connection
            $db = JFactory::getDbo();
            // Create a new query object.
            $query = $db->getQuery(true);
            // Insert columns.
            $columns = array('user_id', 'document_name');

            // Insert values.
            $values = array($useid, $db->quote($filename) );
            // Retrieve the shout
            $query
                    ->insert($db->quoteName('#__documents'))
                    ->columns($db->quoteName($columns))
                    ->values(implode(',', $values));
            $db->setQuery($query);
            $db->execute();
            JFactory::getApplication()->enqueueMessage(JText::_('DOCUMENT SUCCESFULLY UPLOADED'), 'Message');

           } else {
             echo "error !"; //Redirect and throw an error message
           }
        } else {
           echo "Wrong extension !"; //Redirect and notify user file is not right extension
        }
         return $dest ;

    }
 }

/tmpl/default.php

<form  method="post" enctype="multipart/form-data" >
      <div class="form-group">
        <label for="highestacademic">Attachment of Highest Academic Qualifications</label>
        <input name="highestacademic" type="file" id="highestacademic">
        <p class="help-block">Degree, Diploma, Pre-University, GCE O-Levels, etc.</p>
      </div>
      <div class="form-group">
        <label for="language">Attachment of Language Qualifications</label>
        <input name="language" type="file" id="language">
        <p class="help-block">TOEFL, IELTS , etc.</p>
      </div>
      <div class="form-group">
        <label for="passport">Attachment of Passport Copy</label>
        <input name="passport" type="file" id="passport">
        <p class="help-block">ID or Passport copies both sides.</p>
      </div>
      <div class="form-group">
        <label for="supporting">Other Supporting Documents</label>
        <input name="supporting" type="file" id="supporting">
        <p class="help-block">Other supporting documents ie photos ,etc .</p>
      </div>
       <button type="submit" class="btn btn-default">Submit</button>
    </form>
2

I finally solved this by using $jinput->files->get() as in below :

Note the doc[1] which puts all upload files in an array in the Jinput object here $files = $jinput->files->get('docs');

helper.php

class moddocumentsHelper
{
    /**
     * Retrieves the hello message
     *
     * @param   array  $params An object containing the module parameters
     *
     * @access public
     */
    public static function getdata()
    {
        $jinput = JFactory::getApplication()->input;
       //Retrieve file details from uploaded file, sent from upload form
        //$file = JRequest::getVar('highestacademic', null, 'files', 'array');
         $files = $jinput->files->get('docs');
        //Import filesystem libraries. Perhaps not necessary, but does not hurt
         jimport('joomla.filesystem.file');
        foreach($files as $key => $file ){
          if($file['name'] !== " "){
             // //Clean up filename to get rid of strange characters like spaces etc
               $filename =  uniqid().$file['name'];
              // //Set up the source and destination of the file
                $src = $file['tmp_name'];
                $dest = JPATH_BASE . DS . "media" . DS ."documents" . DS . $filename;



              // //First check if the file has the right extension, we need jpg only
               if ( strtolower(JFile::getExt($filename) ) == 'jpg' || strtolower(JFile::getExt($filename) ) == 'png' || strtolower(JFile::getExt($filename) ) == 'pdf' || strtolower(JFile::getExt($filename) ) == 'doc') {
                   if ( JFile::upload($src, $dest) ) {
                    $user = JFactory::getUser();
                    $useid = $user->id ;
              //     // Obtain a database connection
                    $db = JFactory::getDbo();
              //     // Create a new query object.
                    $query = $db->getQuery(true);
              //     // Insert columns.
                   $columns = array('user_id', 'document_name' ,'type');

              //     // Insert values.
                   $values = array($useid, $db->quote($filename) , $db->quote($key));
              //     // Retrieve the shout
                   $query
                           ->insert($db->quoteName('#__documents'))
                           ->columns($db->quoteName($columns))
                           ->values(implode(',', $values));
                   $db->setQuery($query);
                   $db->execute();

              //     // Redirect to a page of your choice
              //     // header("Location: http://".$_SERVER["HTTP_HOST"]."/administrator");
                 } else {
                  echo "error !"; //Redirect and throw an error message
                 }
               } else {
                 echo "Wrong extension !"; //Redirect and notify user file is not right extension
              }
          }

        }


         return $files ;

    }



}

tmpl/default.php

 <form  method="post" enctype="multipart/form-data" >
      <div class="form-group">
        <label for="highestacademic">Attachment of Highest Academic Qualifications</label>
        <input name="docs[1]" type="file" id="highestacademic">
        <p class="help-block">Degree, Diploma, Pre-University, GCE O-Levels, etc.</p>
      </div>
      <div class="form-group">
        <label for="language">Attachment of Language Qualifications</label>
        <input name="docs[2]" type="file" id="language">
        <p class="help-block">TOEFL, IELTS , etc.</p>
      </div>
      <div class="form-group">
        <label for="passport">Attachment of Passport Copy</label>
        <input name="docs[3]" type="file" id="passport">
        <p class="help-block">ID or Passport copies both sides.</p>
      </div>
      <div class="form-group">
        <label for="supporting">Other Supporting Documents</label>
        <input name="docs[4]" type="file" id="supporting">
        <p class="help-block">Other supporting documents ie photos ,etc .</p>
      </div>
       <button type="submit" class="btn btn-default">Submit</button>
    </form>
  • More sensible to not repeat method/function calls on the same string. if ( strtolower(JFile::getExt($filename) ) == 'jpg' || strtolower(JFile::getExt($filename) ) == 'png' || strtolower(JFile::getExt($filename) ) == 'pdf' || strtolower(JFile::getExt($filename) ) == 'doc') { becomes if (in_array(strtolower(JFile::getExt($filename)), ['jpg', 'png', 'pdf', 'doc'])) { – mickmackusa Jun 15 at 12:37
  • Since you have resolved your own question, please award the green tick to your answer. – mickmackusa Jun 15 at 12:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.