簡體   English   中英

如何在codeigniter上傳base64圖片

[英]How to upload base64 image on codeigniter

我正在使用 php,我在 api 中得到 Base64 圖像,我想保存/存儲到數據庫中並想將圖像上傳到服務器中,我該怎么做? 我嘗試使用以下代碼但出現以下錯誤“無法打開內容,Http 作者不支持可寫連接”

function imageupload()
{
     $data = json_decode(file_get_contents("php://input"), TRUE);
     $files=file_get_contents($_FILES["file"]["tmp_name"]); 
     $image = base64_decode($files);
     $image_name = md5(uniqid(rand(), true));
     $filename = $image_name . '.' . 'png';
     $path = base_url().'upload/blog/';
     file_put_contents($path . $filename, $image);
}

從路徑中刪除base_url()

要么

看一下這個

jQuery:

$(document).on('click', '#upload', function () {
    let form_data = new FormData();
    let data_url = document.getElementById('my_image').toDataURL('image/png');
    data_url = data_url.replace(/^data:image\/(png|jpg|jpeg);base64,/, '');
    form_data.append('uploaded_image', data_url);
    $.ajax({
        url: 'upload-avatar',
        method: 'post',
        data: form_data,
        dataType: 'json',
        contentType: false,
        async: true,
        processData: false,
        cache: false
    });
});

PHP:

$img = $this->request->getPost('uploaded_image'); // for ci4
$img = $this->input->post('uploaded_image'); // for ci3
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace('data:image/jpeg;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$file_data = base64_decode($img);
file_put_contents(/* my_path and my_file_name */, $file_data);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM