Manual test of uploaded file size - size not returned

chris.paschen

Chris Paschen
Due to several different reasons (way too complex to explain) we need to manually review the uploads to several file upload elements.
One of the things that we need to check is to make sure the file size being upload is not over a certain limit.

I'm calling the following code within a function of the form with a PHP onBeforeProcess:
Code:
 $fileCoverFile = $formModel->getElementData('ked_books_books___upload_book_cover_image');
  $fileInfo = new SplFileInfo($fileCoverFile);
  $extension = $fileInfo->getExtension();
  $filename = $fileInfo->getFilename();
  $validFileType = 0;

  $fileAbsolute = $fileInfo->getRealPath();
  $absoluteFile = new SplFileInfo($fileAbsolute);
  $size = $absoluteFile->getSize();
[I originally just tried to use the getSize() directly on $fileInfo but got nothing there.]

However, no matter what I do I can't actually get the size of the file.
I've also tried running this after the entry has been saved (onAfterProcess) with same results.

Is there any other way to get the size of the file that someone is trying to upload?
(unforutnately using validations or the in-built element size limits won't work for this situation).

Any ideas why the above code isn't working?
 
Just wondering, is there any way to get the size of the file from Fabrik (like we can get _raw, etc.) at the point of onBeforeProcess (or onAfterProcess)?
 
It isn't working 'cos onBeforeProcess fires before we upload ...

https://github.com/Fabrik/fabrik/blob/master/components/com_fabrik/models/form.php#L1232

As you can see, we fire onBeforeProcess, do the uploads (which will update the form data with the destination path), then fire onBeforeStore.

So you can either run your plugin onBeforeStore, or access PHP's $_FILES array directly. The uploaded file data (including the size) should be in $_FILES['ked_books_books___upload_book_cover_image'] ...

$size = $_FILES['ked_books_books___upload_book_cover_image']['size'];

Although it'll only be there if a file was actually uploaded, so you'd need to do the usual sanity checking to make sure it exists.

-- hugh
 
Thanks. That's what I figured was wrong (I was doing things in the wrong order/at the wrong time in the processing).

Just curious ... one of the things that my code is doing is checking to see if the file size is excessively huge. If so, I don't actually want to upload it to the server. Is there really any way to do that (actually know the file size BEFORE it is actually uploaded) without doing some sort of client-side js or something? (i.e. is the file size 'communicated' to the element at some point in the process where I can check it before it actually gets uploaded to the server)?
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top