简体   繁体   English

将base64图像上传为图像文件

[英]Uploading a base64 image as an image file

So I'm receiving an image in base64 encoding. 因此,我正在接收base64编码的图像。 I want to decode the image and upload it in a specific directory. 我想解码图像并将其上传到特定目录。 The encoded file is an image and I'm using $file = base64_decode($base64_string); 编码的文件是图像,我正在使用$file = base64_decode($base64_string); to decode it. 对其进行解码。 Uploading it via file_put_contents('/uploads/images/', $file); 通过file_put_contents('/uploads/images/', $file); always results in failed to open stream: No such file or directory error. 总是导致failed to open stream: No such file or directory错误。

So, how do I take the decoded string, and upload it on a specific path? 那么,如何获取解码后的字符串并将其上传到特定路径?

Thanks. 谢谢。

Your path is wrong. 您的路线是错误的。

/uploads/images

is checking for a folder in the / directory called uploads. 正在/目录中查找名为uploads的文件夹。 You should specify the full path to the folder: 您应该指定文件夹的完整路径:

/var/www/vhosts/MYSITE/uploads/images

I had the same problem and finally solved using this: 我遇到了同样的问题,并最终解决了这个问题:

<?php
$file = base64_decode($base); 
$photo = imagecreatefromstring($file);

//create a random name 
$RandomStr = md5(microtime());
$name = substr($RandomStr, 0, 3);
$name .= date('Y-m-d');
$name .= '.jpg';

//assign name and upload your image
imagejpeg($photo, 'images/'.$name, 100);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM