1

In my custom component/admin, in the item edit template I am trying to let the user upload a file.

The problem is that the file is not being passed in my save function. There is not even in the array as key. If I remove the enctype="multipart/form-data" from the form (which is wrong because no file will be passed) then the field comes with the filename as string.

[docfile] => my_file_to_upload.docx

but with the enctype="multipart/form-data" then, the result is :

2018-07-28T07:10:12+00:00       DEBUG 127.0.0.1 save_override_function_in_model Data $data : Array
(
    [id] => 2
    [title] => test
    [alias] => test-test
    [catid] => 0
    [client_id] => 240
    [published] => 0
    [created] => 2018-07-20 06:07:43
    [tags] => 
)

Here is the form in the tmpl/edit.php

<form action="<?php echo JRoute::_('index.php?option=com_comtest&layout=edit&id=' . (int) $this->item->id); ?>" method="post"
      id="adminForm" enctype="multipart/form-data" class="form-validate">
  <div class="form-horizontal">
    <fieldset class="adminform">
      <?php echo JHtml::_('bootstrap.startPane', 'myTab', array('active' => 'details')); ?>
      <?php
      echo JHtml::_('bootstrap.addPanel', 'myTab', 'details', empty($this->item->id) ?
                      JText::_('COM_COMTEST_NEW_CONTRACT', true) :
                      JText::sprintf('COM_COMTEST_EDIT_CONTRACT', $this->item->id, true));
      ?>
      <div class="row-fluid">
        <?php echo JLayoutHelper::render('joomla.edit.title_alias', $this); ?>
      </div>
      <div class="row-fluid">
        <div class="span6">
          <?php echo $this->form->renderField('catid'); ?>
          <?php echo $this->form->renderField('client_id'); ?>
          <?php echo $this->form->renderField('published'); ?>
          <?php echo $this->form->renderField('created'); ?>
          <?php echo $this->form->renderField('docfile'); ?>
        </div>
      </div>
      <?php echo JHtml::_('bootstrap.endPanel'); ?>
      <input type="hidden" name="task" value="" />
      <?php echo JHtml::_('form.token'); ?>
      <?php echo JHtml::_('bootstrap.endPane'); ?>
    </fieldset>
  </div>
</form>

The field in the xml models/forms/ is :

<field name="docfile" type="file" label="Select File" description="Select a doc/docx file to upload" 
         size="40" accept=".doc, .docx" />

and here is the overriden save function in the model

public function save($data = null, $key = null) {
    JRequest::checkToken() or die('Invalid Token');
    $file = JFactory::getApplication()->input->get('docfile');
    jimport('joomla.filesystem.folder');
    jimport('joomla.filesystem.file');

    JLog::add('Post $_POST : ' . print_r($_POST, TRUE), JLog::DEBUG, 'save_override_function_in_model');
    JLog::add('Data $data : ' . print_r($data, TRUE), JLog::DEBUG, 'save_override_function_in_model');
    JLog::add('File $file : ' . print_r($file, TRUE), JLog::DEBUG, 'save_override_function_in_model');

    return parent::save($data);
}

and theese are the log entries from the save function :

2018-07-28T07:18:04+00:00       DEBUG 127.0.0.1 save_override_function_in_model Post $_POST : Array
(
    [jform] => Array
        (
            [title] => test
            [alias] => test-test
            [catid] => 0
            [client_id] => 240
            [published] => 0
            [created] => 2018-07-20 06:07:43
        )

    [task] => testcom.apply
    [8c0a826f880ab2cd2842de2040510c6d] => 1
)

2018-07-28T07:18:04+00:00       DEBUG 127.0.0.1 save_override_function_in_model Data $data : Array
(
    [id] => 2
    [title] => test
    [alias] => test-test
    [catid] => 0
    [client_id] => 240
    [published] => 0
    [created] => 2018-07-20 06:07:43
    [tags] => 
)

2018-07-28T07:18:04+00:00       DEBUG 127.0.0.1 save_override_function_in_model File $file : Array
(
    [0] => 
)

1 Answer 1

2

I found the problem which was the way I was getting the file in the model. So I just changed the following line :

$file = JFactory::getApplication()->input->get('docfile');

with

$file = JFactory::getApplication()->input->files->get('jform', null, 'raw');

And this way I get an array with all the files.

1
  • 1
    Also, replace JRequest with JSession. The former is deprecated and will be removed in Joomla 4
    – Lodder
    Jul 30, 2018 at 7:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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