简体   繁体   中英

How to save multiple decoded base64 images into database in php?

I am designing a app in phonegap. I send multiple base64 images to php webservice. Now i need to decode all those base64 images and save them into database.

I am hoping for the best solution. Thank you.

This is my code for assigning base64 values into a hidden input type.

for(i = 0; i< image.length;i++){
    $('#table_postad').append('<input type="hidden" value="'+image[i]+'"    name="ad_image'+i+'" class="ad_image"/>');
    imageArray.push(document.getElementsByClassName("ad_image")[i].value);
}

Following is the code to connect server:

var server =  'http://example.com/webServiceForProject/';
function sendDataToServer(){
alert("access");
var datas = $("#form_description").serialize();//form_description is id for form
console.log(datas);
$.ajax({
    type: 'POST',
    data: datas,
    url: server+'insert.php',
    success: function(data){
        alert(data);
    },
    error: function(){
        alert('There was an error adding your comment');
    }   
});

}

this is php code:

<?php
    define('UPLOAD_DIR', 'images/');
    $adPhotos = array();
    $i=0;
    while(isset($_POST["ad_image".$i])){
        array_push($adPhotos,($_POST["ad_image".$i]));
        $i++;
        echo($adPhotos[i]);
    }
    $j = 0;
    while(sizeof($adPhotos)){
    $adPhotos[$j]= str_replace('data:image/png;base64,', '', $adPhotos[$j]);
    $adPhotos[$j]= str_replace(' ', '+', $adPhotos[$j]);
    $file[$j] = UPLOAD_DIR . uniqid() . '.png';
    $success = file_put_contents($file[$j], $data[$j]);
        j++;
    }
    //insert code here.....

?>

使用php的base64_decode解码图像并将其保存到数据库中(例如使用mysql_queryINSERT INTO... -statement。

Ram, you don't have to convert images to base64. Please use this code.

<script type="text/javascript" charset="utf-8">
        document.addEventListener("deviceready", onDeviceReady, false);
        function onDeviceReady() {}
        function browseImage() {
            navigator.camera.getPicture(uploadPhoto, function(message) {
            alert('get picture failed');
        },{
            quality: 80, 
            destinationType: navigator.camera.DestinationType.FILE_URI,
            sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY
        }
            );

        }
        function uploadPhoto(imageURI) {
            var options = new FileUploadOptions();
            options.fileKey="file";
            options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
            options.mimeType="image/jpeg";
            var params = new Object();
            params.value1 = "test";
            params.value2 = "param";
            options.params = params;
            options.chunkedMode = false;
            var ft = new FileTransfer();
            ft.upload(imageURI, "http://example.com/api/upload.php", win, fail, options);
        }
        function win(r) {
            console.log("Response Code = " + r.responseCode);
            console.log("Response = " + r.response);
            console.log("Sent = " + r.bytesSent);
        }
        function fail(error) {
            console.log("Error: Code = " = error.code);
        }
    </script>

    <input type="button" onclick= "browseImage()" value="Browse Image" />

    //Server: PHP code to upload file
    $img_name = $time."jpg";
    if(move_uploaded_file($_FILES["file"]["tmp_name"], "folder/".$img_name)){ //upload files in location folder/
        //use mysql query to save the filename. (mysql_query("insert into tbl_name(image) values('$img_name')"))
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM