XF 2.0 Check for server max file size on upload forms?

Jaxel

Well-known member
I made an addon... it has an upload form. My server only allows files up to a certain size.

If the user uploads a file larger than the max server size, the form returns the following error:
Oops! We ran into some problems. Please try again later. More error details may be in the browser console.


Then of course, in the browser console, it dumps the entire content of a page... hidden somewhere in that is:
Security error occurred. Please press back, refresh the page, and try again.


How do we check file sizes on ajax upload forms?
 
You can probably use ini_get('upload_max_filesize'), but XenForo will throw friendly errors if you set XF's max file upload size to something at or below PHP's already
 
How does that check file sizes? That just gives me a number.

Displaying the maximum file size to the user is not the issue... the issue is when they ignore the maximum file size and upload something bigger anyways.
 
Well, realistically you won't be able to check the filesize, at least AFAIK, before it's uploaded. Maybe with javascript, but I doubt it
 
I just need the form uploader to give an understandable error to the end user.

Right now, if a user uploads something out of bounds, it just returns the generic errors which I posted in the first post. This isn't helpful to the end user at all. They don't know what error they got... nor why there was an error in the first place.

There doesn't seem to be a way to catch an error related to uploading something too big.
 
You'd need to detect it client side, as the file that's being uploaded there is bigger than post_max_filesize, so most of the submitted data isn't even reaching XF properly.

This means you need JS to be involved in the upload, which isn't necessarily trivial, though I haven't looked into what is involved in reading data directly from input[type=file].
 
We detect the value on the server side and expose that to the attachment JS, which blocks even attempting to upload any file that we know will fail.
 
Forgetting attachments... lets look at editing resource icons in the RM. That has a check for file sizes. I've looked at the code, and I don't see any JS involved with that.
 
There isn't any client side checking there, so if you uploaded a large enough image, the same thing would very likely happen.
 
Okay, I see how you did it in attachment_manager.js

Question though... how do you popup the generic error from Javascript as if you were doing:
Code:
throw new \XF\PrintableException(\XF::phrase(''));
 
The attachment system doesn't involve a server request (it blocks before that). That sort of overlay is done via XF.alert().
 
Cool!
Code:
            if (XF.config.uploadMaxFilesize > 0 && $upload[0].files[0].size > XF.config.uploadMaxFilesize)
            {
                XF.alert(XF.phrase('file_too_large_to_upload'));
                $upload.val('');
            }
 
Back
Top Bottom