Friday 16 May 2014

How to upload & Save Files with new name




<?php

 /* 
   //files array details
    $_FILES['easyfile']['name'] - the actual name of the uploaded file.

    $_FILES['easyfile']['size'] - the size in bytes of the uploaded file.

    $_FILES['easyfile']['tmp_name']- the uploaded file in the temporary directory on the web server.
   
    $_FILES['easyfile']['type'] - the MIME type of the uploaded file. file type like image,documnet,excel file

    $_FILES['easyfile']['error'] - the error code associated with this file upload.

*/
$msg="";
$upload_limit=1024000;
$valid_file=false;
echo $dirpath = dirname(__FILE__);
if (isset($_POST['submit']))
{

if($_FILES['easyfile']['name'])
{
    //if no errors...
    if(!$_FILES['easyfile']['error'])
    {
   
    //here is file destils
   
    echo "<pre>";
    print_r($_FILES);
    echo "</pre>";
   
    if($_FILES['easyfile']['size'] < $upload_limit ) //can't be larger than  upload limit
        {
            $valid_file = true;
           
        }else
        {

            $msg = 'Your file size is to big.';
        }
       
        //if the file has passed the test
        if($valid_file)
        {
            echo $file_new_name =  time()."_".strtolower($_FILES['easyfile']['name']); //rename your file

            //move/copy it to where we want it to be

            move_uploaded_file($_FILES['easyfile']['tmp_name'], $dirpath.'/uploads/'.$file_new_name);

            $msg = 'Congratulations!  Your file was upload successfully!.';
        }
   
   
    }
}

}// if submit button is hit

if ($msg!="")
echo $msg;
?>


<form action="" method="post" enctype="multipart/form-data">
    Your file: <input type="file" name="easyfile" size="40" />
    <input type="submit" name="submit" value="Submit" />
</form>

File Uploading in PHP

<?php

if (isset($_POST['submit']))
{


if($_FILES['easyfile']['name'])
{
    //if no errors...
    if(!$_FILES['easyfile']['error'])
    {
   
    //here is file destils
   
    echo "<pre>";
    print_r($_FILES);
    echo "</pre>";
   
   
    }
}

}// if submit button is hit

?>


<form action="" method="post" enctype="multipart/form-data">
    Your file: <input type="file" name="easyfile" size="40" />
    <input type="submit" name="submit" value="Submit" />
</form>

List files in directory using PHP

<?php

//List files in directory using PHP

$dirpath = dirname(__FILE__);// path to directory
if(is_dir($$dirpath))
{
    if($handle = opendir($$dirpath))
    {
        while(($file = readdir($handle)) !== false)
        {
            if($file != "." && $file != ".." && !is_dir($file))
            {
                echo $file.'<br/>';
            }
        }
        closedir($handle);
    }
}

?>

Wednesday 7 May 2014

Get country information 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>";
 
}


echo "country code ".visitor_country('27.255.252.191');
?>

Get Current Dollar 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";
?>

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

?>
Freelance Jobs