Part of the EllisLab Network
   
1 of 7
1
Extended form_validation: now with file checking :)
Posted: 21 July 2009 04:36 PM   [ Ignore ]  
Lab Assistant
RankRank
Total Posts:  102
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 01:15 PM   [ Ignore ]   [ # 1 ]  
Lab Assistant
RankRank
Total Posts:  104
Joined  12-09-2007

Has anyone tried this? Sounds very good!

Profile
 
 
Posted: 29 July 2009 01:20 PM   [ Ignore ]   [ # 2 ]  
Lab Assistant
RankRank
Total Posts:  102
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 02:19 PM   [ Ignore ]   [ # 3 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  389
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 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: 29 July 2009 07:08 PM   [ Ignore ]   [ # 4 ]  
Lab Assistant
RankRank
Total Posts:  102
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 09:13 AM   [ Ignore ]   [ # 5 ]  
Grad Student
Rank
Total Posts:  39
Joined  01-08-2009

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

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

Change line 95 to

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

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

Profile
 
 
Posted: 31 July 2009 10:38 AM   [ Ignore ]   [ # 7 ]  
Lab Assistant
RankRank
Total Posts:  102
Joined  01-23-2009
GlennJ - 31 July 2009 09:22 AM

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 10:45 AM   [ Ignore ]   [ # 8 ]  
Grad Student
Rank
Total Posts:  39
Joined  01-08-2009

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

Profile
 
 
Posted: 31 July 2009 11:00 AM   [ Ignore ]   [ # 9 ]  
Lab Assistant
RankRank
Total Posts:  102
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 08:51 AM   [ Ignore ]   [ # 10 ]  
Grad Student
Avatar
Rank
Total Posts:  43
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
 
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 721, on January 06, 2010 09:38 AM
Total Registered Members: 115007 Total Logged-in Users: 61
Total Topics: 122440 Total Anonymous Users: 4
Total Replies: 647313 Total Guests: 503
Total Posts: 769753    
Members ( View Memberlist )