首页 > 解决方案 > 如何用php替换字符串并上传

问题描述

我想从base64 png上传图像并用不同的图像类型重命名。

if ($img) { 
    $img = str_replace('data:image/png;base64,', '', $img);
    $img = str_replace(' ', '+', $img);
    $data = base64_decode($img);
    $file = UPLOAD_DIR . $name . = str_replace("image/", ".", $type); 
    $success = file_put_contents($file, $data); 
    }

标签: php

解决方案


尝试使用file_get_contents它内置的 base64数据 uri 协议包装器

if ($img) {
    $contents = file_get_contents($img); // $img = 'data:image/png;base64,....'
    // setting $file name etc.
    $success = file_put_contents($file, $contents);
}

推荐阅读