3

I am making a basic MVC component, mostly following the HelloWorld tutorial. I made a form which contains a upload button and an input of type "file". The form loads fine, but when I upload a file I am unable to save and open the file for processing. I have tried multiple approaches but none worked. How one does one deal with uploaded file upload?

Controller:

class CabControllerCab extends JControllerLegacy {

public function upload(){

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

    var_dump($file); // does not work, returns null
    }
}

Model:

class CabModelCab extends JModelAdmin{
public function getForm($data = array(), $loadData = false)
{
    $form = $this->loadForm(
        'com_cab.cab',
        'cab',
        array(
            'control' => 'jform',
            'load_data' => $loadData)
    );

    if (empty($form)){
        print 'empty form';
        return false;
    }

    return $form;
    }
}

model/forms/cab.xml

<?xml version="1.0" encoding="UTF-8"?>
<form>
<fieldset>
    <field
    name="fileData"
    type="file"
    label="COM_CAB_CAB_FILE_LABEL"
    description="COM_CAB_CAB_FILE_DESC"
    size="40"
    class="inputbox"
    default=""
    />
</fieldset>
</form>

view.html.php

class CabViewCab extends JViewLegacy {
public function display($tpl = null){

    // get the Data
    $form = $this->get('Form');

    // Assign the Data
    $this->form = $form;

    $this->addToolbar();

    parent::display($tpl);
    }

protected function addToolbar(){
    JToolbarHelper::title('Import Members');
    }
}

tmpl/default.php

<h1>Default View Layout</h1>
<form action="<?php print JRoute::_('index.php?option=com_cab&view=cab&task=upload')?>"
method="post" name="adminForm" id="adminForm" enctype="multipart/form-data">
<div class="form-horizontal">
    <fieldset>
        <legend>Legend</legend>
        <div class="span6">
            <?php foreach ($this->form->getFieldset() as $field)?>
            <div class="control-group">
                <div class="control-label">
                    <?php print $field->label; ?>
                </div>
                <div class="controls">
                    <?php print $field->input; ?>
                    <button id="upload-submit" class="btn btn-primary">
                        <i class="icon-upload icon-white"></i>
                        Start Upload
                    </button>
                </div>
            </div>
        </div>
    </fieldset>
    <div>
    <input type="hidden" name="task" value="cab.upload" />
        <?php echo JHtml::_('form.token'); ?>
    </div>
</div>
</form>
  • you could try building your component with: component-creator.com They have a (basic) upload field aswell. You can learn a lot from their code, at least I did... – COBIZ webdevelopment Apr 2 '15 at 9:57
2

I figured it out while studying another component which had a basic upload function. I needed to make the following change in my controller.

public function upload() {
    $app = JFactory::getApplication();

    $data = $app->input->files->get('jform', array (), 'array');

    $fileName = $data ['fileData'] ['name'];
    $tmpFileName = $data ['fileData'] ['tmp_name'];

    // handle the file here
}

Your Answer

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