Hey Guys,

In this blog we will see how to handle validation for ‘post_max_size’ and 'memory_limit' in Joomla for file uploads. Sometimes when dealing with file uploads in Joomla or in any PHP frameworks, form post size can exceed the max allowed ‘post_max_size’ (set in php.ini in your server configuration). If the size of post data exceeds post_max_size, the $_POST and $_FILES superglobals are emptied. Now, if post size validation is not added, it becomes very difficult for the end user as well as developer to track what exactly the issue is.

Using the code given below for 'post_max_size' and 'memory_limit' validation we can display the warning if post data is greater than ‘post_max_size’ or if the 'memory_limit' is exceeded in the same way as ‘com_media’ in Joomla does.

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))
{
	// Show warning and return
	JError::raiseWarning(100, “Total size of upload exceeds the limit.”);
	return false;
}

  

b2ap3_thumbnail_oie_815511jDoPNs0N_20141009-060335_1.pngHope 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.