Wednesday, May 7, 2014

get all images as array from a folder or directory in php

<?php
$myfull_path="/uploads";
$images=array();

if ($dir = opendir( $myfull_path)) {
    $images = array();
    while (false !== ($file = readdir($dir))) {
        if ($file != "." && $file != ".." )
        {
        $ext=strtolower(getExtension($file));
       
        if ($ext=="jpg" || $ext=="jepg" || $ext=="png" || $ext=="gif" || $ext=="bmp" )   
        {

            $images[] = $file;
        }
       
        }
    }
    closedir($dir);
}



print_r($images);

function getExtension($str)
{
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
}

?>

Get Dollar current rate with INR



<?php
function current_rate($price=1)
{
  
        $url  = "http://www.google.com/finance/converter?a=$price&from=USD&to=INR";
       $data = file_get_contents($url);
         preg_match("/<span class=bld>(.*)<\/span>/",$data, $converted);
            
     $new_price = preg_replace("/[^0-9.]/", "", $converted[1]);
    

return    $new_price;
}




echo "1$==".$current_dolar_rate=current_rate(1) ." INR";
?>

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";   
}

?>

check valid Extension in php

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

    function easy_checkvalid_ext($ext,$allowed_ext)
    {
     
     if($allowed_ext[$ext])
        {
            return true;
        }
       else
        {
           return false;
       }  
    
       }

 $validfile = easy_checkvalid_ext('jpg',$allowed_image_format);

if ( !$validfile)
echo "not a valid file type";
?>

Get country code by ip address in php

<?php

function get_easy_visitor_country($ip_address)
{
    if (!$ip_address)
     $ip_address=$_SERVER['REMOTE_ADDR'];
   
     $geopluginURL='http://www.geoplugin.net/php.gp?ip='.$ip_address;
     $addrDetailsArr =@unserialize(file_get_contents($geopluginURL));
      easy_debug($addrDetailsArr);
     $result=$addrDetailsArr['geoplugin_countryCode'];
    return $result;
}


function easy_debug($obj)
{

echo "<pre>";
     print_r(  $obj);
     echo "</pre>";
 
}

?>