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

[eluser]Chicken's Egg[/eluser]
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:

I added a few lines to the function file_max_size() to make sure that the $max_size variable actually is an integer.
Code:
// 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].

Code:
// 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.

Code:
// 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;
  }
#12

[eluser]devbro[/eluser]
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.
#13

[eluser]Chicken's Egg[/eluser]
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:
Code:
$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',
    );
#14

[eluser]devbro[/eluser]
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() Tongue )
#15

[eluser]GlennJ[/eluser]
I noticed a bug which stops checkboxes being validated.

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

Code:
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']];


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

    }

Seems to work ok so far...
#16

[eluser]Johan André[/eluser]
It's almost impossible to read when you don't use code-tags...
#17

[eluser]GlennJ[/eluser]
Sorry, my first post was too long and it lost the end code tag, should be ok now...
#18

[eluser]devbro[/eluser]
[quote author="GlennJ" date="1249498545"]I noticed a bug which stops checkboxes being validated.

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

Code:
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...[/quote]


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.
#19

[eluser]GlennJ[/eluser]
The validation line was

Code:
$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?
#20

[eluser]devbro[/eluser]
[quote author="GlennJ" date="1249508108"]The validation line was

Code:
$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?[/quote]

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




Theme © iAndrew 2016 - Forum software by © MyBB