File upload rename error

kouwearie

Member
Im getting an error when im trying to rename a file when uploading.
I use the following code:


PHP:
import( 'joomla.filesystem.file' );
// Get values from input fields
$dier_id = $formModel->formData['dier_id'];
$naam_dier = $formModel->formData['naam_dier']; 
$achternaam = $formModel->formData['achternaam'];

// Split the original filename and get the file extension
$old = ($formModel->formData['foto_huisdier']);
$oldParts = pathinfo($old); $extensie = $oldParts['extension'];

// Combine the input field values to create the new filename
$nieuw_bestand = $naam_dier . '' . $achternaam . '' . $dier_id . '.' . $extensie;

// Move the original file to the new location and give it a new name
JFile::move(JPATH_SITE.$old, JPATH_SITE.$nieuw_bestand);

// Update the form data and the database with the new filename
$formModel->updateFormData('foto_huisdier', $nieuw_bestand, true); 
$object = new stdClass();
$object->id = $formModel->formData['dier_id'];
$object->bestandsnaam = $nieuw_bestand;
$result = JFactory::getDbo()->updateObject('klant_diergegevens', $object, 'dier_id');


Any suggestions?
 
Last edited:
Hi @kouwearie , it's more likely I can't help, but nonetheless I have some remarks:
  • The format of your code isn't easy to read.
  • What kind of error do you receive?
  • Did you enter the code in form php-plugin?
 
Im gonna try this now
Code:
use Joomla\CMS\Filesystem\File;

// Get values from input fields
$dier_id = $formModel->getData()->get('dier_id');
$naam_dier = $formModel->getData()->get('naam_dier');
$achternaam = $formModel->getData()->get('achternaam');

// Split the original filename and get the file extension
$old = ($formModel->getData()->get('foto_huisdier'));
$oldParts = pathinfo($old);
$extensie = $oldParts['extension'];

// Combine the input field values to create the new filename
$nieuw_bestand = $naam_dier . '_' . $achternaam . '_' . $dier_id . '.' . $extensie;

// Move the original file to the new location and give it a new name
File::move(JPATH_SITE . '/' . $old, JPATH_SITE . '/' . $nieuw_bestand);

// Update the form data and the database with the new filename
$formModel->getData()->set('foto_huisdier', $nieuw_bestand);
$object = new stdClass();
$object->id = $formModel->getData()->get('dier_id');
$object->bestandsnaam = $nieuw_bestand;
$result = JFactory::getDbo()->updateObject('klant_diergegevens', $object, array('dier_id' => $dier_id));
 
For ajax uploading im still working on the code

Code:
use Joomla\CMS\Factory;
use Joomla\CMS\Filesystem\File;

// Get the database object depending on the Joomla version
$db = version_compare(\Joomla\CMS\Version::MAJOR_VERSION, "4", ">=") ? Factory::getContainer()->get('DatabaseDriver') : Factory::getDbo();

// Get values from input fields
$dier_id = $formModel->getData()->get('dier_id');
$naam_dier = $formModel->getData()->get('naam_dier');
$achternaam = $formModel->getData()->get('achternaam');


// Get the uploaded file
$file = $_FILES['file'];

// Set the upload directory
$uploadDir = JPATH_SITE . '/images/huisdieren/';

// Create a new filename using the input field values and the original file extension
$oldParts = pathinfo($file['name']);
$extensie = $oldParts['extension'];
$nieuw_bestand = $naam_dier . '_' . $achternaam . '_' . $dier_id . '.' . $extensie;

// Move the uploaded file to the new location with the new filename
move_uploaded_file($file['tmp_name'], $uploadDir . $nieuw_bestand);

// Update the form data and the database with the new filename and other input fields
$formModel->getData()->set('foto_huisdier', $nieuw_bestand);

$object = new stdClass();
$object->id = $formModel->getData()->get('dier_id');
$object->naam_dier = $naam_dier;
$object->achternaam = $achternaam;
$object->bestandsnaam = $nieuw_bestand;

$result = $db->updateObject('klant_diergegevens', $object, array('dier_id' => $dier_id));
 
Last edited:
I am getting this error:


{"error":"Error. Unable to upload file."}<br />
<b>Warning</b>: session_write_close(): Failed to write session data using user defined save handler. (session.save_path: /opt/alt/php81/var/lib/php/session) in <b>/var/www/vhosts/d.nl/sub.domainname.nl/libraries/vendor/joomla/session/src/Storage/NativeStorage.php</b> on line <b>114</b><br />

tried modding the php.ini but didnt work... Waiting on webhost to update settings.

after that Probably working ajax file upload rename in a joined and repeated group
 
I can see those "session" messages (which are breaking all Ajay responses) if J! System debug is on (so for testing fileupload switch it off).

"unable to upload file":
Is the upload itself working (without any rename/move code)?
Do you have non-default element settings?
Any special characters in the file name?
 
Last edited:
And i entered the code in the file upload element
You have seen this?
upload_2023-4-30_17-22-48.png


The code in your 1st post (from WIKI) is for standard fileupload, running in an additional form php plugin
Example - File rename on upload (top)
  • This is an example of one way to rename a file on upload (this example assumes Fabrik 3.1+)
  • In a php form plugin add the following to the onBeforeCalculations event (or a later event):
  • You can adjust the values to meet your needs - this is just one possible example
 
You have seen this?
View attachment 20607

The code in your 1st post (from WIKI) is for standard fileupload, running in an additional form php plugin

Maybe something like this works for the ajax uploading:

Code:
<form id="uploadFormname" enctype="multipart/form-data">
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload File" name="submit">
</form>


$(document).ready(function() {
    // handle form submission
    $('#uploadFormname').submit(function(event) {
        event.preventDefault();

        // create a FormData object to store the file and other form data
        var formData = new FormData();
        var file = $('#fileToUpload')[0].files[0];
        formData.append('fileToUpload', file);
        formData.append('my_id', $('input[name="my_id"]').val());
        formData.append('input_element1', $('input[name="input_element1"]').val());
        formData.append('input_element2', $('input[name="input_element2"]').val());

        // send the file via AJAX
        $.ajax({
            url: 'index.php?option=com_example&task=uploadFile',
            type: 'POST',
            data: formData,
            processData: false,
            contentType: false,
            success: function(response) {
                console.log(response);
                // handle success response
            },
            error: function(xhr, status, error) {
                console.log(xhr.responseText);
                // handle error response
            }
        });
    });
});
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top