将base64编码的图像保存到PHP文件中 [英] Save base64 encoded image into the file in PHP

查看:230
本文介绍了将base64编码的图像保存到PHP文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

<?php
header('Content-Type: image/png');
$data = "iVBORw0KGgoAAAANSUhEUgAAAuAAAAI8CAYAAACwIh5dAAAgAElkZJgRCXxOAuIPzzTlg......";       
define('UPLOAD_DIR', '/home/Desktop/image.png');
$img = $data;
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR . uniqid() . '.png';
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file.';
?>

此处$data包含base-64中图像的编码字符串.我想将图像保存到文件中.我之前的代码也通过了Google的许多教程,但无法正常工作.

Here $data contain the encoded string of image in base-64. I want to save the image into the file. I did the previous code also go through lot of tutorial in google but it's not working.

谁能帮助我我要去哪里哪里

Can anyone help me where I am going wrong.

谢谢.

推荐答案

首先指定正确的路径,例如:

First specify the correct path like :

define('UPLOAD_DIR', '/home/Desktop/');

在检查您提供的路径是否设置了读/写权限之后.

After check that the path you provided has read/write permission set or not.

这篇关于将base64编码的图像保存到PHP文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆