如何在codeigniter上上传base64图像

mi7gmzs6  于 2022-12-16  发布在  其他
关注(0)|答案(1)|浏览(207)

我正在使用PHP,我正在获取API中的Base64图像,我想保存/存储到数据库中,并希望上传图像到服务器,我怎么能做到这一点?我尝试了以下代码,但得到以下错误“无法打开内容,Http writer doest not support writetable connections”

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);
}
1zmg4dgp

1zmg4dgp1#

从路径中删除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);

相关问题