javascript - php无法解码js发过来的base64图片编码,有诡异空格
巴扎黑
巴扎黑 2017-04-11 13:11:36
0
4
442

我通过vue的axois通过post方法向php转图片编码,在php base64_decode解码的时候总是失败

发现

  1. 在php里,base64编码里的'+'号变成了空格

  2. 尝试用str_replace(' ', '+', $str)失败,无法替换

  3. 尝试用str_replace(' ', '%2B', $str)成功替换,还是无法解码

  4. 对比初始的base64编码和替换后的编码发现中间少了几行(我认为原因就出在这里)

  5. 后面尝试
    (1)在js里先将'+'号替换到'-',再在php里替换回来,同样上面3和4

(2)在js里使用encodeURIComponent,再在php里解码回来,同样上面3和4

啊啊啊,我主要是做前端的,php这方面不是很熟悉,查了很久资料都没找到解决方法,相当难受

代码:
JavaScript:

...
    this.$http.post('../info/publish.php', {
              name: this.name,
              description: this.description,
              cid: this.fenlei,
              price: this.price,
              image: this.base64,
              address: this.location
            }).then((res) => {
    //          console.log(this.base64);
              console.log(res.data);
            });
...

PHP:

...
$data = file_get_contents('php://input');
    $pName=json_decode($data)->name;
    $pDe=json_decode($data)->description;
    $cid=json_decode($data)->cid;
    $price=json_decode($data)->price;
    $image=json_decode($data)->image;
    $image1=str_replace(' ', '+', $image);
    // $image=urldecode($image);
    $address=json_decode($data)->address;
    $image=substr(strstr($image1,','), 1);
    $img=base64_decode($image);
...

巴扎黑
巴扎黑

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!