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;
  }
#22

[eluser]devbro[/eluser]
There is an issue here check this link:
http://ca3.php.net/manual/en/features.fi...errors.php

lookup these:
UPLOAD_ERR_INI_SIZE
UPLOAD_ERR_FORM_SIZE

As you can see if the file size is already violated then the file will not be available.

Will it be ok if I use your _let_to_num($v) in the new version of the code?
#23

[eluser]quest13[/eluser]
It seems to solve most of the upload issues in a simple and effective way. Please can you clarify on following points as I am new to php and CI.


1.Should I save this extension of class validation as a separate file in library ? or simply I can paste it on the 'form_validation' library ?


2.In view, we give set_value for text type. How to give set_value for file type ? Is it the same like text type ?

I tried it by creating a separate file and included in the library.It didn't work.( I did include that library in controller)and I tried set_value and that too didn't work.

Any help in this regard from any one of you is highly appreciated.

Thanks in advance.
#24

[eluser]GlennJ[/eluser]
1) You should extend the the class, don't change the CI code as you'll have a nightmare if you want to upgrade to the latest version.

2) You can't set the value of a file field, due to security reasons (e.g. you could set it to a file they didn't specify and grab it from their system!).
#25

[eluser]Chicken's Egg[/eluser]
Quote:Will it be ok if I use your _let_to_num($v) in the new version of the code?
Of course, but you might be even more interested in this one:

Code:
function let_to_bit($sValue)
  {
    // Split value from name
    if(!preg_match('/([0-9]+)([ptgmkb]{1,2}|)/ui',$sValue,$aMatches))
    { // Invalid input
      return FALSE;
    };
    if(empty($aMatches[2]))
    { // No name -> Enter default value
      $aMatches[2] = 'KB';
    };
    if(strlen($aMatches[2]) == 1)
    { // Shorted name -> full name
      $aMatches[2] .= 'B';
    }
    $iBit   = (substr($aMatches[2], -1) == 'B') ? 1024 : 1000;
    // Calculate bits:
    switch(strtoupper(substr($aMatches[2],0,1))){
      case 'P':
        $aMatches[1] *= $iBit;
      case 'T':
        $aMatches[1] *= $iBit;
      case 'G':
        $aMatches[1] *= $iBit;
      case 'M':
        $aMatches[1] *= $iBit;
      case 'K':
        $aMatches[1] *= $iBit;
        break;
    }

    // Return the value in bits
    return $aMatches[1];
  }

  // The following lines were used to test the above function:
  echo '1 '.let_to_bit('')    . '<br />'; // returns FALSE
  echo '2 '.let_to_bit('2')   . '<br />'; // returns 2048
  echo '3 '.let_to_bit('2K')  . '<br />'; // returns 2048
  echo '4 '.let_to_bit('2L')  . '<br />'; // returns 2048
  echo '5 '.let_to_bit('2Kb') . '<br />'; // returns 2000
  echo '6 '.let_to_bit('2KB') . '<br />'; // returns 2048

I wrote the function for your extension, so please feel free to use it if you like.
#26

[eluser]devbro[/eluser]
[quote author="quest13" date="1249651776"]
2.In view, we give set_value for text type. How to give set_value for file type ? Is it the same like text type ?
[/quote]

To approach this and reduce the headache of uploading files on each form_validation failure you can do this:

1. if file had no problem copy it to temp folder and keep the link to it in session.
2. on your form if the link is specified then make the field optional.
#27

[eluser]quest13[/eluser]
I was away from code igniter forum for the past one month as I was busy doing a project in wordpress. I found a tremendous difference between CI and wordpress or any other framework as a matter of fact,in terms of forum response and technical bench strength. The response from CI is just overwhelming.I sometimes wonder whether someone is already started replying my question when I just start typing in the forum ! The response is so quick and so prompt.
I have become a die-hard fan of CI now.

Thanks once again for my file upload validation query. But I still have one problem.In my view page, I have only "name" and "file" mentioned, not the size. But in file_required function, it checks for the 'size'(===0).It means I have to tell about the size also in my view page as a mandatory requirement ?

Thanks
#28

[eluser]GlennJ[/eluser]
The size is handled automatically, all you need is the file field in you view. PHP will then generate a global variable with all the details of the uploaded file(s), 'size' is one of the attributes.

See http://us3.php.net/manual/en/reserved.va....files.php for more information.
#29

[eluser]devbro[/eluser]
For Those of you that like this class and are using it I have a news Smile
The new version of the code is now completed. you can check it out at:
http://devbro.com/testing/ci_form_validation/

I have added a few new rules:
file_min_size
file_disallowed_type
file_image_mindim
file_image_maxdim

let me know what you think of the new code.
#30

[eluser]Chicken's Egg[/eluser]
Great work Devbro. I'm Looking forward to upgrade to version 2. :p




Theme © iAndrew 2016 - Forum software by © MyBB