Post size validation in Joomla!

Hey Guys,

I am blogging about "post size validation". The purpose of writing this blog is many users complain about files not getting uploaded and gives no error for that; so, if your users are having same problem you can use this code to get rid of it.

As we know if the size of post data is greater than post_max_size, the $_POST and $_FILES superglobals are empty. Now, if post size validation is not added in this case it becomes very difficult for the end user as well as developer to track what exactly the issue is. So, now using post size validation code; we can display the warning if post data is greater than post_max_size.

If you are developing an extension for Joomla then you can add the following code in the controller file.

Let’s Code:

// Total length of post back data in bytes.
$contentLength = (int) $_SERVER['CONTENT_LENGTH'];

// Maximum allowed size of post back data in MB.
$postMaxSize = (int) ini_get('post_max_size');

// Maximum allowed size of script execution in MB.
$memoryLimit = (int) ini_get('memory_limit');

// Check for the total size of post back data.
if (($postMaxSize > 0 && $contentLength > $postMaxSize * 1024 * 1024) || ($memoryLimit != -1 && $contentLength > $memoryLimit * 1024 * 1024))
{
        JError::raiseWarning(100, “Total size of upload exceeds the limit.”);
}

b2ap3_thumbnail_oie_815511jDoPNs0N.png

Hope this helps! :)

*Note : post_max_size value set in php.ini is max size of post data allowed. This setting also affects file upload. To upload large files, this value must be larger than upload_max_filesize.
If memory limit is enabled by your configure script, memory_limit also affects file uploading.
Usually, memory_limit should be larger than post_max_size.
When an integer is used, the value is measured in bytes.

Read more

© 2024 Extly, CB - All rights reserved.