【问题标题】:How to send base64 image to server - PHP如何将base64图像发送到服务器 - PHP
【发布时间】:2016-03-09 00:29:01
【问题描述】:

我正在通过 PHP 文件从 Android 应用程序向服务器发送 base64 字符串。
我从一个教程视频中得到了这个:

<?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);
    }
?>

它在目录中存储图像,但是当您打开时,它没有图像。这是一个空白的png。代码有问题吗?如果是这样,我应该使用什么?谢谢。

【问题讨论】:

  • 你能贴出-$encoded_string返回的字符串格式吗?检查是否需要对其进行操作
  • 您是从哪个教程中获得的?从安全的角度来看,这是一场灾难……几乎任何人都可以将他们想要的任何内容写入您的服务器磁盘。此外,通常没有理由使用 base64...您增加了 33% 的开销并浪费了内存。

标签: php android sql-server base64


【解决方案1】:

有几点你应该检查:

1.确保服务器收到base64字符串

$encoded_string = $_POST['encoded_string'];

检查$encoded_string的长度,应该和android客户端说的一样。

  1. 确保解码正常

    $decoded_string = base64_decode($encoded_string);

检查$decoded_string 的长度,它不应该是零或奇怪的东西。

  1. 确保写入的文件有效

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

$is_written应该是已经写入文件的数据长度,如果是false,那就有问题了。

【讨论】:

  • 肯定有问题。因为即使我更改代码并手动输入硬编码值,图像仍然是空白的。
  • 在哪里?你能编辑问题并向我们展示你做了什么吗?
  • 我意识到它正在工作。由于某种原因,当我下载图像时它是空白的。但我不打算下载它,所以没关系。但是很高兴知道我应该检查我的代码的事情,我是 php 新手。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-04
  • 1970-01-01
  • 1970-01-01
  • 2018-09-21
  • 1970-01-01
  • 2011-11-18
相关资源
最近更新 更多