douhaoqiao9304 2016-03-09 00:29
浏览 57
已采纳

如何将base64映像发送到服务器 - PHP

I'm sending a base64 string from an Android app to a server through a PHP file.
I got this from a tutorial video:

<?php
    require 'Init.php';
    header('Content-type : bitmap; charset=utf8');

    if(isset($_POST['encoded_string'])){

        $encoded_string = $_POST['encoded_string'];
        $image_name = $_POST['image_name'];

        $decoded_string = base64_decode($encoded_string);

        $path = 'ProfileIcons/' .$image_name;

        $file = fopen($path, 'wb');

        $is_written = fwrite($file, $decoded_string);
        fclose($file);
    }
?>

It's storing the image in the directory but when you open, it has no image. It's a blank png. Is there something wrong with the code? If so, what should I use? Thanks.

  • 写回答

1条回答 默认 最新

  • doulao5916 2016-03-09 01:02
    关注

    There are several points you should check:

    1.Make sure the server has received the base64 string

    $encoded_string = $_POST['encoded_string'];

    Check the length of $encoded_string, it should have the same length as the android client says.

    1. Make sure the decoding works

      $decoded_string = base64_decode($encoded_string);

    Check the length of $decoded_string, it should NOT be zero or something odd.

    1. Make sure the file written works

      $is_written = fwrite($file, $decoded_string);

    $is_written should be the length of the data that has been written to the file, if it is a false, then something is wrong.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥20 sim800c模块 at指令及平台
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计