Php base64 encode uploaded file

As @Sjoerd and @zerkms correctly point out, you do not need to do this - a blob column is be used to store raw binary data, so you can bypass the Base64 process and insert the raw data of the file.

If you want to store images in a database (which, by the way, I personally don't like to do) it is better to store the raw data - Base64 encoding the data makes it larger, and means that it must be decoded before the image is rendered, which adds processing overhead.

This is how you can insert the raw binary data (assuming the MySQL extension):

$data = file_get_contents($_FILES['name_of_control']['tmp_name']);
$data = mysql_real_escape_string($data);

$query = "
  INSERT INTO table
    (`blob_column`)
  VALUES
    ('$data')
";

mysql_query($query);

If you really do want to Base64 encode it (in which case it could just be stored in a varchar), just just add a base64_encode() call:

$data = file_get_contents($_FILES['name_of_control']['tmp_name']);
$data = base64_encode($data);

// There is an argument that this is unnecessary with base64 encoded data, but
// better safe than sorry :)
$data = mysql_real_escape_string($data);

$query = "
  INSERT INTO table
    (`varchar_column`)
  VALUES
    ('$data')
";

mysql_query($query);

In this tutorial, we will let you know how to handle the image encoded with Base64 and write the image to folder. 
While we have working with API for mobile or web application , You will notice that they will send the format of images in Base64 encoded. So in that case, you will need to move the Base64 encoded image to server as a image file.and have to save it in the folder

Example Core PHP:

 

Upload base64 to file in Codeigniter 

Are you looking for a way to save a PNG image server-side, from a base64 data string, or unable to upload to a base64 encoded image using Codeigniter? We can help you with that.

We are creating some function for using base64 string check is valid or not and size and file type

create file application/libraries/Base64fileUploads.php

is_base64($base64string) == true 
        ){  
            $base64string = "data:image/jpeg;base64,".$base64string;
            $this->check_size($base64string);
            $this->check_dir($path);
            $this->check_file_type($base64string);
            
            /*=================uploads=================*/
            list($type, $base64string) = explode(';', $base64string);
            list(,$extension)          = explode('/',$type);
            list(,$base64string)       = explode(',', $base64string);
            $fileName                  = uniqid().date('Y_m_d').'.'.$extension;
            $base64string              = base64_decode($base64string);
            file_put_contents($path.$fileName, $base64string);
            return array('status' =>true,'message' =>'successfully upload !','file_name'=>$fileName,'with_path'=>$path.$fileName);
        }else{
            print_r(json_encode(array('status' =>false,'message' => 'This Base64 String not allowed !')));exit;
        }
    }
    
    public function check_size($base64string){
        $file_size = 8000000;
        $size = @getimagesize($base64string);
        
        if($size['bits'] >= $file_size){
            print_r(json_encode(array('status' =>false,'message' => 'file size not allowed !')));exit;
        }
        return true;
    }
    
    public function check_dir($path){
        if (!file_exists($path)) {
            mkdir($path, 0777, true);
            return true;
        }
        return true;
    }
    
    public function check_file_type($base64string){
        $mime_type = @mime_content_type($base64string);
        $allowed_file_types = ['image/png', 'image/jpeg', 'application/pdf'];
        if (! in_array($mime_type, $allowed_file_types)) {
            // File type is NOT allowed
           // print_r(json_encode(array('status' =>false,'message' => 'File type is NOT allowed !')));exit;
        }
        return true;
    }
}

Example Codeigniter :

Call this class Codeigniter in controller 


$base64file   = new Base64fileUploads();
$return = $base64file->du_uploads($this->data['document'],$document_front_site);

How to convert uploaded file to base64 in PHP?

“upload image and convert to base64 php” Code Answer.
$path = 'myfolder/myimage.png';.
$type = pathinfo($path, PATHINFO_EXTENSION);.
$data = file_get_contents($path);.
$base64 = 'data:image/' . $ type . '; base64,' . base64_encode($data);.

How do you base64 encode in PHP?

Syntax. base64_encode() function can encode the given data with base64. This encoding is designed to make binary data survive transport through transport layers that are not 8-bit clean such as mail bodies. Base64-encoded data can take about 33% more space than original data.

How to convert a image to base64?

How to convert image to Base64 online.
Choose the source of image from the “Datatype” field..
Paste the URL or select an image from your computer..
If necessary, select the desired output format..
Press the “Encode image to Base64” button..
Download or copy the result from the “Base64” field..

How can I download base64 image in PHP?

header('Content-Disposition: attachment;filename="test. png"'); header('Content-Type: application/force-download'); echo base64_decode($base64strImg);