Upload file in Joomla 4! custom component

1,183 views
Skip to first unread message

Samy

unread,
Jul 9, 2022, 4:29:30 AM7/9/22
to Joomla! General Development
Hi Joomlers,

I have a custom component for Joomla 3 that have field to upload file in the backend.
I'm using function in controllers/statushistory.php file to save the data and upload the file then redirect to table list.
All of the fields can works normally, $data can be saved in the database, $file can be uploaded in spesific directory.

Now I want to upgrade it to Joomla 4 :)
After struggle to repair some errors, finally my component can works in Joomla 4, except upload file's field.

This is my models/forms/statushistory.xml 
```
<field name="image"
            type="file"
            label="Upload image"
            description="Select a JPG/JPEG/PNG file to upload"  
            directory="updatestatus"
            enctype="multipart/form-data"
             size="40"
             default=""
            accept=".jpg, .jpeg, .png"
/>
```

This is my controllers/statushistory.php
```
jimport('joomla.application.component.controllerform');

use Joomla\Image\Image;
use Joomla\Filesystem\File;
use Joomla\CMS\Factory;

class CouriertrackerControllerStatushistory extends JControllerForm {
       function __construct() {
              parent::__construct();
       }

public function save($key = NULL, $urlVar = NULL) {

jimport('joomla.filesystem.folder');
jimport('joomla.filesystem.file');

$input = JFactory::getApplication()->input;
$data = $input->post->get('jform', null, 'array');

// Create the uploads folder if not exists in /images folder
if ( !JFolder::exists( JPATH_SITE . "/images/updatestatus/" ) ) {
       JFolder::create( JPATH_SITE . "/images/updatestatus/" );
}

$file = $input->files->get('jform', null, 'raw'); //I've try using  $input->files->get('jform', null, 'array') with no luck

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

$filename   = JFile::stripExt($filenames); // get filename
$ext             = JFile::getExt($filenames); // get file extensions

$src    = $file['tmp_name'];

if ( $filename != '' ) {
     $filepath = JPath::clean(JPATH_SITE . '/images/updatestatus/');
     $newfilename = strtolower(preg_replace('/[\s+\+.]/', '_', $filename ));

$dest = $filepath . $newfilename .'.'. $ext;

$data['image'] = strtolower(preg_replace('/[\s+\+.]/', '_', $newfilename )) .'.'. $ext;

$input->post->set('jform', $data);
parent::save();
$this->setredirect('/redirecttolist...');
}

```

Please need help... 
What part of this script that need to change or modified so it can upload file in J4! 
Thanks a lot

Regards,
(Samy)

Cliff

unread,
Jul 16, 2022, 3:56:37 AM7/16/22
to Joomla! General Development
Try:         $files = $input->files->get('jform', array(), 'array');

Much of your code is J3 syntax - I am losing contact with that.

Tomas Dalebjörk

unread,
May 23, 2023, 9:45:21 AM5/23/23
to Joomla! General Development
This is what I was looking for :)

But where is the actual file uploaded data located of the file submitted by the form?

Regards Tomas

Tomas Dalebjörk

unread,
May 23, 2023, 9:48:45 AM5/23/23
to Joomla! General Development
No, that didn't work, as the there are no "jform" data in the files, as it is here:
$files = $input->get('jform')

lördag 16 juli 2022 kl. 09:56:37 UTC+2 skrev Cliff:
Reply all
Reply to author
Forward
0 new messages