Heim  >  Artikel  >  Backend-Entwicklung  >  php yii2接口中图片上传

php yii2接口中图片上传

WBOY
WBOYOriginal
2016-08-04 09:19:372035Durchsuche

现在在给ios给接口,遇到了一些图片上传的问题,现在可以接收到ios传来的base64编码后的数据。我是否需要解码?解码又需要怎么处理才能把图片上传到服务器,并从服务器获取URL来存入到数据库呢?求大神指导。本人是在yii2框架下写的接口

回复内容:

现在在给ios给接口,遇到了一些图片上传的问题,现在可以接收到ios传来的base64编码后的数据。我是否需要解码?解码又需要怎么处理才能把图片上传到服务器,并从服务器获取URL来存入到数据库呢?求大神指导。本人是在yii2框架下写的接口

我在前端做canvas图片合成的时候用过base64
yii2处理
1.读取base64编码后的数据 => $data
2.$data = base64_decode($data);//data为二进制数据了
3.假设web目录下有upload文件夹,文件名为md5(microtime(true)).'.jpg' => $filename
4.file_put_contents(Yii::$app->basePath.'/web/upload/'.$filename,$data);
5.url = Yii::$app->urlManager->createAbsoluteUrl(['/']).'upload/'.$filename

php yii2接口中图片上传php yii2接口中图片上传

第一个图是ios传来的经过base64编码后的数据,第二个是我base64_decode()解码后输出的数据。这些是我在postman上模拟的http传输。为什么解码后的数据就是乱码了呢?

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn