Part of the EllisLab Network
   
1 of 7
1
Extended form_validation: now with file checking :)
Posted: 21 July 2009 06:36 PM   [ Ignore ]  
Lab Assistant
RankRank
Total Posts:  110
Joined  01-23-2009

I have been thinking of doing this for a long time and finally did smile.
http://devbro.com/testing/ci_form_validation/


I extended the form_validation to now have ability to check $_FILES (file uploading).

here is the way it can be used:

$this->form_validation->set_rules('image','Image','file_required|file_size_max[500]|file_allowed_type[image]'); 

I added 3 set of rules so far:
1. file_required: same as required
2. file_size_max: define maximum file size in KB
3. file_allowed_type: what type of file to expect.

So what do you think? is there any way to improve it further?

Profile
 
 
Posted: 29 July 2009 03:15 PM   [ Ignore ]   [ # 1 ]  
Lab Assistant
RankRank
Total Posts:  115
Joined  12-09-2007

Has anyone tried this? Sounds very good!

Profile
 
 
Posted: 29 July 2009 03:20 PM   [ Ignore ]   [ # 2 ]  
Lab Assistant
RankRank
Total Posts:  110
Joined  01-23-2009

i used it myself in two projects already. so far no problem and it saves me writing callback_ with file upload.

Profile
 
 
Posted: 29 July 2009 04:19 PM   [ Ignore ]   [ # 3 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  455
Joined  03-28-2008

I’m gonna try this one out!
If it works good (which I’m sure it will do) you are truly awesome!

 Signature 

———————————————————————————————————————————-
Imac 27” Core i7 / 12GB RAM
Macbook Pro 15” C2D 2.53Ghz / 4GB RAM / NVIDIA GeForce 9400M + 9600M GT 512MB
iPhone 4 16Gb Black

http://www.rockkarusellen.se

Profile
 
 
Posted: 29 July 2009 09:08 PM   [ Ignore ]   [ # 4 ]  
Lab Assistant
RankRank
Total Posts:  110
Joined  01-23-2009

I moved the location of the source code:
http://devbro.com/testing/ci_form_validation/

hope it helps.

Profile
 
 
Posted: 31 July 2009 11:13 AM   [ Ignore ]   [ # 5 ]  
Grad Student
Rank
Total Posts:  88
Joined  01-08-2009

Nice one! Just what I was looking for, will give this a go now.

 Signature 

Web Design Essex - Essex SEO - Essex Alloy Wheel Repair

Profile
 
 
Posted: 31 July 2009 11:22 AM   [ Ignore ]   [ # 6 ]  
Grad Student
Rank
Total Posts:  88
Joined  01-08-2009

Change line 95 to

$result $rule($postdata['name']); 

and you’ll get rid of the PHP notice cool smile

 Signature 

Web Design Essex - Essex SEO - Essex Alloy Wheel Repair

Profile
 
 
Posted: 31 July 2009 12:38 PM   [ Ignore ]   [ # 7 ]  
Lab Assistant
RankRank
Total Posts:  110
Joined  01-23-2009
GlennJ - 31 July 2009 03:22 PM

Change line 95 to

$result $rule($postdata['name']); 

and you’ll get rid of the PHP notice cool smile

The issue is that if I do that then I would have problem if more fields are needed for that validation rule. also if there are more validations they will all fail.

Profile
 
 
Posted: 31 July 2009 12:45 PM   [ Ignore ]   [ # 8 ]  
Grad Student
Rank
Total Posts:  88
Joined  01-08-2009

All $_FILE entries have the [‘name’] field, so I don’t see the problem?

 Signature 

Web Design Essex - Essex SEO - Essex Alloy Wheel Repair

Profile
 
 
Posted: 31 July 2009 01:00 PM   [ Ignore ]   [ # 9 ]  
Lab Assistant
RankRank
Total Posts:  110
Joined  01-23-2009

what if meta_data, size, temp_dir are the indexes that are needed? in case of allowed types name is no longer needed, just the mime type. If some sort of content checking is needed then temp_dir is needed.

I was thinking of passing the field name instead but there may be things that need to be modified half way. Something like xss_clean or fix_mime_type or change_filename

Profile
 
 
Posted: 02 August 2009 10:51 AM   [ Ignore ]   [ # 10 ]  
Grad Student
Avatar
Rank
Total Posts:  44
Joined  07-06-2008

Thank you for sharing this code devbro! I like your library-extension. However, I did make some changes too. If you don’t mind, I’ll post them here. red face

I added a few lines to the function file_max_size() to make sure that the $max_size variable actually is an integer.

// Check if $max_size is valid
    
if (preg_match("/[^0-9]/"$max_size))
    
{
      
return FALSE;
    

And I changed the function file_allowed_type() in order to make it possible to allow a few file-types. The file-types are seperated by commas. So a validation rule in my controller can look like file_allowed_type[word,jpeg].

// FUNCTION: file_allowed_type
  // Checks if an uploaded file has a certain file type
  //
  // @Author: devbro (21 July 2009)
  // @Author: chicken's egg (2 August 2009) (edited)
  //
  // @access  public
  // @param   $file        array     File-information
  // @param   $type        string    Allowed filetype(s)
  // @return  bool
  
function file_allowed_type($file,$sType)
  
{
    
/*
     three types allowed:
     1. mime type image/gif
     2. general mime type image application
     3. file ext pdf,xls
    */
    // Create an array of allowed filetype
    
$aTypes explode(',',$sType);

    
// Validate file-type
    
foreach($aTypes AS $key => $sType)
    
{
      $sType 
trim($sType);
      if(
strpos($file['type'],$sType)!==FALSE)
      
{
        
return TRUE;
      
}
    }
    
// File-type not found? Invalid filetype
    
$this->set_message('file_allowed_type',"%s cannot be {$file['type']}.(should be %s)");
    return 
FALSE;
  

Which brings me to a question. Why are we using $file[‘type’]? I discovered that the value of $file[‘type’] - at least on my Windows XP computer - is prescribed by the file-extension. So this function gives the same results, but is easier to handle as most of us do know file-extensions better then mime-types.

// FUNCTION: file_allowed_type
  // Checks if an uploaded file has a certain extension.
  //
  // @Author: chicken's egg (2 August 2009)
  //
  // @access  public
  // @param   $file        array     File-information
  // @param   $type        string    Allowed filetype(s)
  // @return  bool
  
function file_allowed_type($file,$sType)
  
{
    
// Create an array of allowed file-type
    
$aTypes explode(',',strtolower(trim($sType)));
    
// Take the extension of the uploaded file
    
$sExtension strtolower(substr($file['name'],(strrpos($file['name'],".") + 1)));
    
// Check if it is an allowed filetype
    
if(in_array($sExtension,$aTypes))
    
{
      
return TRUE;
    
}
    
// Filetype not found? Invalid filetype
    
$this->set_message('file_allowed_type',"%s cannot be {$file['type']}.(should be %s)");
    return 
FALSE;
  
Profile
 
 
   
1 of 7
1