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)