file upload validation GIF JPG BMP SWF CMYK RGB PNG required required minimum size bytes maximum size bytes disabled enabled

file validation

The file validator helps validating files and  even image file types. It relies on the built-in getimagesize() function.

Tips:

  • The validator also helps to limit the filesize, the maximum filesize defaults to the upload_max_filesize setting of PHP.
  • Set the required parameter to zero (false) if file upload is not mandatory.
  • To make validation extra safe you can use the imagecreatecheck option, which uses the imagecreatefrom...() GD functions to create an image variable of the uploaded file in the memory. If it fails, the image is broken, if it succeeds, you'll most likely won't have problems during converting the image.
  • Please note that you have to make sure that there's enough memory for your PHP process to create large image variables using imagecreatecheck! If in doubt, take a look at this PHP memory limit calculator for image resizing!

<?php  

$config 
= Array(    

  
'element' => Array(    

    
'type' => 'inputFile',    
    
// ...further element settings here...    

    
'validation' => Array(     

      Array(
        
'type'     => 'file',
        
'types'    => Array('gif','jpgrgb','png','bmp','swf'),
          
// feel free to finetune jpg validation:
          //   - jpgrgb for JPEGs with RGB channel
          //   - jpgcmyk for JPEGs with CMYK channel
          //     (some browsers (IE) have problems with 
          //      displaying CMYK JPGs, best to avoid!)
          //   - jpg for both jpgrgb and jpgcmyk

        
'imagecreatecheck' => 0// default is 0,
        
'minimum'  => 123,
        
'maximum'  => 4900// default value is: ini_get('upload_max_filesize')
        
'required' => 0// default is 1
        
'help'     => 'Help message value for this validator only'
      
)
    )
  )
);

?>


Live examples:

form validation for developers!

clonefish is a proven, mature form generator class that helps PHP developers to create, validate and process secure, flexible and easy-to-maintain forms
learn more
Bookmark and Share