[eluser]Chicken's Egg[/eluser]
GlennJ, you saved my day! I discovered this evening that a select box which had the rule 'required' was not stopping the form from being submitted when the other input fields were filled in correctly. I felt so happy when I found your solution! But there is one thing I don't understand. Wenn I saw the new code, I was afraid that if I would upload a file other fields would not be validated anymore. But that is certainly not the case. It worked like te charm. But, how come?
Below is also some new code. I altered the function file_size_max() once again. The function now watches the server settings too. If the maximum file size the website developer allows, crosses the limits of the server configuration, his value is overwritten by the value given by the server.
Code:
// FUNCTION: file_size_max
// Checks if the filesize of the uploaded file isn't to big.
//
// @Author: devbro (21 July 2009)
// @Author: Chicken Egg (6 August 2009) (edited)
//
// @access public
// @param $file array File-information
// @param $max_size integer Filesize in Kb
// @return bool
function file_size_max($file,$max_size)
{
// Check if $max_size is valid
if (preg_match("/[^0-9]/", $max_size))
{
return FALSE;
}
// Check if the given maximum filesize is also accepted by the server.
// If not -> alter the value to its maximum
// Step 1: Take the minimum: post_max_size or upload_max_filesize
$max_server_upload_size = min($this->_let_to_num(ini_get('post_max_size')), $this->_let_to_num(ini_get('upload_max_filesize')));
// Step 2: Calculate the max upload size given by the webmaster in bits.
$max_user_upload_size = $max_size * 1024;
// Step 3: Decide which one to use.
$max_size = min($max_server_upload_size,$max_user_upload_size);
// 1 KB = 1024 bits
if($file['size'] > $max_size)
{
$max_size /= 1024;
$this->set_message('file_size_max',"%s is too big. (max allowed is $max_size KB)");
return FALSE;
}
return TRUE;
}
// FUNCTION: _let_to_num()
// This function transforms the php.ini notation for
// numbers (like '2M') to an integer. (bits)
//
// Author: gilthans dot NO dot SPAM at gmail dot com
// Published: 03-Dec-2007 06:52
// Source: php.net - http://www.php.net/manual/en/ini.core.php#79564
//
// @param $v string String container filesize in letters.
// @output integer Filesize in bits.
private function _let_to_num($v)
{
$l = substr($v, -1);
$ret = substr($v, 0, -1);
switch(strtoupper($l)){
case 'P':
$ret *= 1024;
case 'T':
$ret *= 1024;
case 'G':
$ret *= 1024;
case 'M':
$ret *= 1024;
case 'K':
$ret *= 1024;
break;
}
return $ret;
}