Wednesday, May 7, 2014

check a valid file extension and size in php

<?php
    $allowed_ext = array(
             'jpg'    =>    true,
            'gif'    =>    true,
            'png'    =>    true
     );




$allowed_imagesize=1024000;

    function easy_checkvalid_ext($ext,$allowed_ext)
    {
     
     
       if($allowed_ext[$ext])
        {
            return true;
        }
       else
        {
           return false;
       }  
    
       }
   
    function getExtension($str)
{
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
}   
 
     


      
    function easy_checkfile_size($size,$allowed_imagesize) {
    
       if($size < $allowed_imagesize)
        {    return true;}
       
       else
        {
             return false;
    
       }
    }

       $ext=getExtension($_FILES["files"]["name"]);    
      $validfile = easy_checkvalid_ext($ext,$allowed_image_format);
      $validfilesize = easy_checkfile_size($_FILES["files"]["size"],$allowed_imagesize);

if (!$validfilesize or  !$validfile)
{
echo "not a valid file format or too big size";   
}

?>

No comments:

Post a Comment