File upload form for custom component Joomla

I have a field in my form that has a file type. When the user clicks on the save icon, I naturally want to upload the file to the server and save the file name in the database. I tried to verify this by repeating the file name, but it does not seem to work. Also, how to add a file name to the database? Is this done in the model? Thank!

Controllers / customcom.php

jimport('joomla.filesystem.file');     
class CustomComControllerCustomCom extends JControllerForm
        {
            function save()
            {
                $file = JRequest::getVar('img_url', null, 'files', 'array');

                $filename = JFile::makeSafe($file['name']);

                echo $filename;
                }
        }

models / forms / customcom.xml

    <?xml version="1.0" encoding="utf-8"?>
    <form enctype="multipart/form-data">
            <fieldset>
                  <field
                        name="img_url"
                        type="file"
                        label="Image upload"
                        description=""
                        size="40"
                        class="inputbox"
                        default=""
                />
            </fieldset>
   </form>
+5
source share
2 answers

Just got it.

The right way

$jinput = JFactory::getApplication()->input;
$files = $jinput->files->get('jform');
$file = $files['img_url'];

That should do the trick. Then the $ file array contains the following keys:

  • Error
  • name
  • the size
  • tmp_name
  • of type

I deleted my original answer as it was misleading.

+7
source

:

, ( )

// Import dependencies
jimport('joomla.filesystem.file')

// Get input
$input = JFactory::getApplication()->input;

// Get uploaded file info
$file_info = $input->files->get('img_url', null);

// This will hold error
$error = null;

// Check if there was upload at all, mime is correct, file size, XSS, whatever...
if (!MycomponentHelper::canUpload($file_info, $error)
{
    $this->setError('problem: ' . $error);
    return false;
}

// Move uploaded file destination
JFile::upload($file_info['tmp_name'], $destination);
+3

All Articles