Part of the EllisLab Network
   
2 of 7
2
Extended form_validation: now with file checking :)
Posted: 02 August 2009 10:57 AM   [ Ignore ]   [ # 11 ]  
Lab Assistant
RankRank
Total Posts:  107
Joined  01-23-2009

nice suggestions chicken egg. I am working on a new set of updates right now. If it is ok with you I will incorporate suggestions into my new code.

My reason for using mime_type was my laziness in the first place. I am planning to at least modify that to look at ext as well. the only issue that I have right now is that some ext and mime_types dont match. lookup mp3 upload in the forum and you get what i mean.

Profile
 
 
Posted: 02 August 2009 04:27 PM   [ Ignore ]   [ # 12 ]  
Grad Student
Avatar
Rank
Total Posts:  43
Joined  07-06-2008

Of course it’s ok to use may code. I am using yours aswell. smile

Yes mp3 is a tricky one. May be an array with the file-extensions as keys and the mime-types as values is an idea, but that would also mean that you would limit the usability of the file-validation-class to the most common used file-types on the internet. For example:

$aFiletypes = array(
      
'bmp'  => 'image/bmp',
      
'doc'  => 'application/msword',
      
'gif'  => 'image/gif',
      
'jpeg' => 'image/jpeg',
      
'jpg'  => 'image/jpeg',
      
'mid'  => 'audio/midi',
      
'midi' => 'audio/midi',
      
'mp3'  => 'audio/mpeg',
      
'pdf'  => 'application/pdf',
      
'txt'  => 'text/plain',
      
'xls'  => 'application/vnd.ms-excel',
      
'zip'  => 'application/zip',
    );
Profile
 
 
Posted: 02 August 2009 04:49 PM   [ Ignore ]   [ # 13 ]  
Lab Assistant
RankRank
Total Posts:  107
Joined  01-23-2009

there is already a similar array in the config folder.

Here are the stuff that I have in mind:
1. disallow_file_type rule
2. fix_mime_type
3. get size from strings ‘10MB’ and ‘4KB’
4. change_file_name (if i do this then no more rename or move after upload->do_upload() raspberry )

Profile
 
 
Posted: 05 August 2009 08:55 AM   [ Ignore ]   [ # 14 ]  
Grad Student
Rank
Total Posts:  42
Joined  01-08-2009

I noticed a bug which stops checkboxes being validated.

This is my fix, showing top and bottom of the function

function _execute($row, $rules, $postdata = NULL, $cycles = 0)
    
{
        
if(isset($_FILES[$row['field']]['size']))
        
{// it is a file so process as a file

            
$postdata = $_FILES[$row['field']];

else
        
{
            parent
::_execute($row, $rules, $postdata,$cycles);        
        
}

    }

Seems to work ok so far…

 Signature 

Web Design Essex - Essex SEO

Profile
 
 
Posted: 05 August 2009 08:58 AM   [ Ignore ]   [ # 15 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  404
Joined  03-28-2008

It’s almost impossible to read when you don’t use code-tags…

 Signature 

———————————————————————————————————————————-
Imac 24” C2D 3.06Ghz / 4GB RAM / Geforce 8800GS 512MB
Macbook Pro 15” C2D 2.53Ghz / 4GB RAM / NVIDIA GeForce 9400M + 9600M GT 512MB
iPhone 3G 16Gb Black

http://www.rockkarusellen.se

Profile
 
 
Posted: 05 August 2009 08:59 AM   [ Ignore ]   [ # 16 ]  
Grad Student
Rank
Total Posts:  42
Joined  01-08-2009

Sorry, my first post was too long and it lost the end code tag, should be ok now…

 Signature 

Web Design Essex - Essex SEO

Profile
 
 
Posted: 05 August 2009 11:30 AM   [ Ignore ]   [ # 17 ]  
Lab Assistant
RankRank
Total Posts:  107
Joined  01-23-2009
GlennJ - 05 August 2009 08:55 AM

I noticed a bug which stops checkboxes being validated.

This is my fix, showing top and bottom of the function

function _execute($row, $rules, $postdata = NULL, $cycles = 0)
    
{
        
if(isset($_FILES[$row['field']]['size']))
        
{// it is a file so process as a file

            
$postdata = $_FILES[$row['field']];
        
}
    
else
        
{
            parent
::_execute($row, $rules, $postdata,$cycles);        
        
}

    }

Seems to work ok so far…


So you basically moved their order of if-statement.

Can you give me the html form that it did caused the code to break please.

Profile
 
 
Posted: 05 August 2009 11:35 AM   [ Ignore ]   [ # 18 ]  
Grad Student
Rank
Total Posts:  42
Joined  01-08-2009

The validation line was

$this->form_validation->set_rules('agree', 'lang:main_agree_terms', 'trim|required');

This is validating a standard HTML checkbox.

The reason it didn’t work before is that if a checkbox is *not* checked it will not be included in the $_POST variable. So you need to check to see if the field is a file upload, if not - process as normal (even if it doesn’t exist in $_POST).

That make sense?

 Signature 

Web Design Essex - Essex SEO

Profile
 
 
Posted: 05 August 2009 11:46 AM   [ Ignore ]   [ # 19 ]  
Lab Assistant
RankRank
Total Posts:  107
Joined  01-23-2009
GlennJ - 05 August 2009 11:35 AM

The validation line was

$this->form_validation->set_rules('agree', 'lang:main_agree_terms', 'trim|required');

This is validating a standard HTML checkbox.

The reason it didn’t work before is that if a checkbox is *not* checked it will not be included in the $_POST variable. So you need to check to see if the field is a file upload, if not - process as normal (even if it doesn’t exist in $_POST).

That make sense?

I got it. Thanks for the explanation. I implement the update to the code.

Profile
 
 
Posted: 06 August 2009 04:27 PM   [ Ignore ]   [ # 20 ]  
Grad Student
Avatar
Rank
Total Posts:  43
Joined  07-06-2008

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.

// 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;
  
}
Profile
 
 
   
2 of 7
2
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 11:15 AM
Total Registered Members: 120406 Total Logged-in Users: 24
Total Topics: 126511 Total Anonymous Users: 6
Total Replies: 665250 Total Guests: 329
Total Posts: 791761    
Members ( View Memberlist )