Welcome Guest, Not a member yet? Register   Sign In
Extended form_validation: now with file checking :)
#21

[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;
  }


Messages In This Thread
Extended form_validation: now with file checking :) - by El Forum - 07-21-2009, 04:36 PM
Extended form_validation: now with file checking :) - by El Forum - 07-29-2009, 01:15 PM
Extended form_validation: now with file checking :) - by El Forum - 07-29-2009, 01:20 PM
Extended form_validation: now with file checking :) - by El Forum - 07-29-2009, 02:19 PM
Extended form_validation: now with file checking :) - by El Forum - 07-29-2009, 07:08 PM
Extended form_validation: now with file checking :) - by El Forum - 07-31-2009, 09:13 AM
Extended form_validation: now with file checking :) - by El Forum - 07-31-2009, 09:22 AM
Extended form_validation: now with file checking :) - by El Forum - 07-31-2009, 10:38 AM
Extended form_validation: now with file checking :) - by El Forum - 07-31-2009, 10:45 AM
Extended form_validation: now with file checking :) - by El Forum - 07-31-2009, 11:00 AM
Extended form_validation: now with file checking :) - by El Forum - 08-02-2009, 08:51 AM
Extended form_validation: now with file checking :) - by El Forum - 08-02-2009, 09:57 AM
Extended form_validation: now with file checking :) - by El Forum - 08-02-2009, 03:27 PM
Extended form_validation: now with file checking :) - by El Forum - 08-02-2009, 03:49 PM
Extended form_validation: now with file checking :) - by El Forum - 08-05-2009, 07:55 AM
Extended form_validation: now with file checking :) - by El Forum - 08-05-2009, 07:58 AM
Extended form_validation: now with file checking :) - by El Forum - 08-05-2009, 07:59 AM
Extended form_validation: now with file checking :) - by El Forum - 08-05-2009, 10:30 AM
Extended form_validation: now with file checking :) - by El Forum - 08-05-2009, 10:35 AM
Extended form_validation: now with file checking :) - by El Forum - 08-05-2009, 10:46 AM
Extended form_validation: now with file checking :) - by El Forum - 08-06-2009, 03:27 PM
Extended form_validation: now with file checking :) - by El Forum - 08-06-2009, 03:38 PM
Extended form_validation: now with file checking :) - by El Forum - 08-07-2009, 02:29 AM
Extended form_validation: now with file checking :) - by El Forum - 08-07-2009, 03:14 AM
Extended form_validation: now with file checking :) - by El Forum - 08-07-2009, 05:22 AM
Extended form_validation: now with file checking :) - by El Forum - 08-07-2009, 05:59 AM
Extended form_validation: now with file checking :) - by El Forum - 08-08-2009, 03:42 AM
Extended form_validation: now with file checking :) - by El Forum - 08-08-2009, 04:10 AM
Extended form_validation: now with file checking :) - by El Forum - 08-08-2009, 10:55 AM
Extended form_validation: now with file checking :) - by El Forum - 08-09-2009, 02:46 PM
Extended form_validation: now with file checking :) - by El Forum - 08-13-2009, 03:45 AM
Extended form_validation: now with file checking :) - by El Forum - 08-13-2009, 03:59 AM
Extended form_validation: now with file checking :) - by El Forum - 08-13-2009, 04:17 AM
Extended form_validation: now with file checking :) - by El Forum - 08-13-2009, 02:50 PM
Extended form_validation: now with file checking :) - by El Forum - 08-16-2009, 03:20 AM
Extended form_validation: now with file checking :) - by El Forum - 08-16-2009, 02:00 PM
Extended form_validation: now with file checking :) - by El Forum - 08-25-2009, 01:00 AM
Extended form_validation: now with file checking :) - by El Forum - 08-25-2009, 01:09 AM
Extended form_validation: now with file checking :) - by El Forum - 08-26-2009, 11:46 PM
Extended form_validation: now with file checking :) - by El Forum - 08-27-2009, 10:51 AM
Extended form_validation: now with file checking :) - by El Forum - 08-27-2009, 04:59 PM
Extended form_validation: now with file checking :) - by El Forum - 08-27-2009, 05:08 PM
Extended form_validation: now with file checking :) - by El Forum - 08-28-2009, 04:08 AM
Extended form_validation: now with file checking :) - by El Forum - 09-06-2009, 11:19 PM
Extended form_validation: now with file checking :) - by El Forum - 09-07-2009, 01:17 AM
Extended form_validation: now with file checking :) - by El Forum - 10-14-2009, 03:28 PM
Extended form_validation: now with file checking :) - by El Forum - 10-14-2009, 08:10 PM
Extended form_validation: now with file checking :) - by El Forum - 10-14-2009, 08:30 PM
Extended form_validation: now with file checking :) - by El Forum - 10-14-2009, 09:40 PM
Extended form_validation: now with file checking :) - by El Forum - 10-14-2009, 10:08 PM
Extended form_validation: now with file checking :) - by El Forum - 10-17-2009, 07:03 PM
Extended form_validation: now with file checking :) - by El Forum - 10-19-2009, 03:12 AM
Extended form_validation: now with file checking :) - by El Forum - 11-02-2009, 04:13 AM
Extended form_validation: now with file checking :) - by El Forum - 11-16-2009, 02:17 PM
Extended form_validation: now with file checking :) - by El Forum - 11-17-2009, 04:09 AM
Extended form_validation: now with file checking :) - by El Forum - 11-17-2009, 10:24 AM
Extended form_validation: now with file checking :) - by El Forum - 11-17-2009, 10:41 AM
Extended form_validation: now with file checking :) - by El Forum - 11-17-2009, 11:06 AM
Extended form_validation: now with file checking :) - by El Forum - 12-20-2009, 06:20 AM
Extended form_validation: now with file checking :) - by El Forum - 12-21-2009, 02:15 AM
Extended form_validation: now with file checking :) - by El Forum - 12-23-2009, 05:32 AM
Extended form_validation: now with file checking :) - by El Forum - 01-23-2010, 12:33 PM
Extended form_validation: now with file checking :) - by El Forum - 02-08-2010, 10:30 PM
Extended form_validation: now with file checking :) - by El Forum - 02-23-2010, 01:32 PM
Extended form_validation: now with file checking :) - by El Forum - 09-02-2010, 12:41 PM
Extended form_validation: now with file checking :) - by El Forum - 11-07-2010, 11:17 PM
Extended form_validation: now with file checking :) - by El Forum - 09-13-2012, 10:27 AM
Extended form_validation: now with file checking :) - by El Forum - 09-13-2012, 11:27 AM
Extended form_validation: now with file checking :) - by El Forum - 09-14-2012, 12:33 PM
Extended form_validation: now with file checking :) - by El Forum - 09-14-2012, 12:44 PM
Extended form_validation: now with file checking :) - by El Forum - 09-14-2012, 12:57 PM



Theme © iAndrew 2016 - Forum software by © MyBB