php - 如何用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);
}
解决方案
尝试使用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);
}
推荐阅读
- docker - 如何在 /mosquitto/data MQTT mosquitto 中存储消息?
- python-3.x - 将新列名映射到 Pandas 中的数据框
- java - 在 Java 的类加载阶段,符号引用如何解析为直接引用?
- azure - 如何在 Azure 数据工厂 V2 中设置看似循环但实际上不是循环的依赖项
- c - 为什么我的动态分配的数组不能在它的函数之外被访问?[C]
- c# - Task.WhenAll() 包含大量任务
- nlp - 如何使 spacy 规则库仅匹配给定模式之间的内容
- ruby-on-rails - 以 Rails 多步骤形式管理时区
- r - 我如何有一个在 r 中乘以 2 个函数的函数?
- wordpress - 仅在 all_items 中显示某些自定义帖子