file upload problem in admin backend - custom component

54 views
Skip to first unread message

Mike eco

unread,
Jul 28, 2018, 7:05:55 AM7/28/18
to Joomla! General Development
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] =>
   
)



Viper

unread,
Jul 28, 2018, 9:44:50 AM7/28/18
to Joomla! General Development
Files available in $_FILES. http://php.net/manual/en/reserved.variables.files.php
And you can access uploaded files as
$files = $this->input->files->get('docfile', array(), 'array');

Maciek Wołpiuk

unread,
Jul 28, 2018, 9:49:46 AM7/28/18
to joomla-de...@googlegroups.com
Try

<?xml version="1.0" encoding="utf-8"?>
<form>
<fieldset>
<field
name="id"
type="hidden"
/>
<field
name="file"
type="file"
label="Enter some text"
description=""
size="10"
/>
</fieldset>
</form>

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

or just var_dump($_FILES);

Beware if U use JFile::upload() some files could be moved, cause Joomla use library to find some unsafe content.


Pozdr
Maciek


--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.
To post to this group, send email to joomla-de...@googlegroups.com.
Visit this group at https://groups.google.com/group/joomla-dev-general.
For more options, visit https://groups.google.com/d/optout.

Mike eco

unread,
Jul 29, 2018, 2:00:11 AM7/29/18
to Joomla! General Development
Thank you @Viper

Mike eco

unread,
Jul 29, 2018, 2:01:44 AM7/29/18
to Joomla! General Development
Thank you Maciek.

It worked perfectly, and the file upload !
Reply all
Reply to author
Forward
0 new messages