PHP for retrieve file extension and file size when upload a file with fileupload element

Keraunox

Member
Hello,

I'm trying to do this:

I have a form with a fileupload element and I want to update two other fields on the form when the form is submited.

the fields are size and extension

i used this code on php plugin of the form onBeforeCalcuation

PHP:
   $filename = ($formModel->formData['file']);
   $ext = pathinfo($filename, PATHINFO_EXTENSION);
   $file_size = filesize(JPATH_SITE.$filename);

   $formModel->updateFormData('document___extension', $ext, true);
   $formModel->updateFormData('document___size', $file_size, true);

The problem is that i'm not able to submit the data retrieved to the db using formModel.

Any help is appreciated

Thank you
 
Last edited:
I used the updateObject method of JDatabaseDriver class instead of the $formModel->updateFormData

I think that I can't update the form as the data are already submitted to the db when the onBeforeCalculation fire so I have to update the db

Am I wrong?

If could be usefull for someone else at the first step with fabrik, joomla and PHP like me here is the full code

PHP:
   //get the id of the current row
   $row_id = $formModel->formData['id'];
   //get the filename
   $filename = ($formModel->formData['file']);
   //get the file extension
   $ext = pathinfo($filename, PATHINFO_EXTENSION);
   //get the file size
   $file_size = filesize(JPATH_SITE.$filename) .' '.'byte';

   // Create an object for the record we are going to update.
   $object = new stdClass();

   // Must be a valid primary key value.
   $object->id = $row_id;
   $object->extension = $ext;
   $object->size = $file_size;

   // Update document table using id as the primary key.
   $result = JFactory::getDbo()->updateObject('document', $object, 'id');
 
Last edited:
Well, onBeforeCalculation runs after we write the data out to the database, so updating the form data won't do much at that point.

Try running it onBeforeProcess, or onBeforeStore.

It is better to do this using our hooks, and modifying our form data prior to us processing and storing it, rather than doing your own database handling after we write out the data.

-- hugh
 
Thank you again for your reply.

I've already tried that events but the code to retrieve file name and filesize don't work as I suppose the file is not yet uploaded to the server ( I'm not using ayax upload).

I'll take a look better at fabrik form php documentation to manage how to get what i need
 
Ok I made some attempts either using ajax download or not with obBeforeStore event to get the file name.
These ones are the working piece of working PHP:

using ajax download (with max file allowed setted to 1) I used this code to retrieve file name

PHP:
   $obj = json_decode($formModel->getElementData('document___file', true , '', 0));
   $filename = $obj[0]->file;

while without ajax I used this code

PHP:
   $filename = $formModel->getElementData('document___file');
 
We are in need of some funding.
More details.

Thank you.

Members online

No members online now.
Back
Top