Search code examples
phpmysqlbase64jquery-file-upload

Jquery File upload to database in base64


I'm having trouble using the Jquery File uploader with bootstrap skin (https://github.com/blueimp/jQuery-File-Upload). The program itself works beautifully as is, even with the database which is functional too as described on github (https://github.com/blueimp/jQuery-File-Upload/wiki/Working-with-databases).

But what I really need is to save the picture in the database in base64 format. I can't seem to get it. So in the add_img function i need to somehow get the pic in base64.

// Standard file uploader code
function add_img($img_name)
{
    $add_to_db = $this->query("INSERT INTO pics (pic_name, pic) VALUES ('".$img_name."','picture in base64 here')");
    return $add_to_db;
}

Solution

  • convert your image to base64 using following code and store it to db.

    $imagedata = file_get_contents("filepath/filename.jpg");
    $base64data = base64_encode($imagedata);
    

    EDIT:

    $file->size = $file_size;//from current code
    
    $imagedata = file_get_contents($file_path);
    $base64data = base64_encode($imagedata);
    $this->add_img($base64data);
    

    Hope this will work for you