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

Chủ Đề