Tuesday 15 April 2014

save images into database using base64encoding

In this tutorial tip let us check how can we save images into database using base64encoding. Usually we will upload an image into the server and keep the image file name into the database, and use that image file name if we want to use that image right?. Yea but here what I’m going to show is how can we save the image itself into the database.


For that Let us create a database table.



CREATE TABLE `test`.`user_pic` (
`id`
INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`img` LONGTEXT NOT NULL ,
`type` VARCHAR( 10 ) NOT NULL
) ENGINE = MYISAM ;

So we are using this table for saving the image.

the next part is the image upload part.



<?php



$hostname = 'localhost';


$dbname = 'test';


$db_user = 'root';


$db_password = '';


$link = mysql_connect($hostname,$db_user,$db_password);


$connect = mysql_select_db($dbname,$link)or die(mysql_error());



if(isset($_FILES['file'])){



    $allowed_file_type = array("jpg", "jpeg", "png", "gif");


    $max_file_size = 524288;



   $file_dir="uploads";



    $file_name = $_FILES['file']['name'];



    $file_size =$_FILES['file']['size'];



    $file_tmp_name =$_FILES['file']['tmp_name'];



    $file_type=$_FILES['file']['type'];



    $ext = pathinfo($file_name, PATHINFO_EXTENSION);



    if (in_array($ext, $allowed_file_type)) {



          if($file_size > $max_file_size){



                $fsize=$max_file_size/1048576;


                echo 'File size must be less than '.$fsize.' MB';


                break;



            }



            $str = file_get_contents($file_tmp_name);


            $b64img=base64_encode($str);


            $sql="INSERT INTO `user_pic` (`img` ,`type`)VALUES ('$b64img', '$ext')";


            mysql_query($sql);



    }


}


?>


<form action="" enctype="multipart/form-data" method="POST">Choose a file to upload : <input type="file" multiple="multiple" name="file" />


 <input type="submit" /></form>



So by using this code you can easly upload an image into the database.





$str = file_get_contents($file_tmp_name);

$b64img=base64_encode($str);




This code is doing the image encoding part. This line of code first read image file then convert it into base64_encode.

If you don’t want to encode it to base64_encode you can remove this line of code.


if you want to get the image from database use this code.




$hostname = 'localhost';


$dbname = 'test';


$db_user = 'root';


$db_password = '';


$link = mysql_connect($hostname,$db_user,$db_password);


$connect = mysql_select_db($dbname,$link)or die(mysql_error());


$sql="SELECT * FROM `user_pic` WHERE `id`=5";


$res=mysql_query($sql);


$data=mysql_fetch_assoc($res);


$image= $data['img'];


$image_type= $data['type'];


$res=base64_decode($image);


header("Content-type:image/$image_type");



So using this simple technique you can easily save images into database using base64encoding. ;)



save images into database using base64encoding