【问题标题】:Upload image from Android app through php move_uploaded_file通过 php move_uploaded_file 从 Android 应用上传图片
【发布时间】:2017-10-09 23:04:51
【问题描述】:

我正在尝试将图像文件从我的 android 应用程序上传到服务器。这是我的 php 文件,它正在处理来自我的网站和 iOS 应用程序的传入图像,用于move_uploaded_file 函数:

<?php include '../../../init.php';

  $post_id = $_GET['post_id'];

  $image_temp   = $_FILES['image']['tmp_name'];
  $image_name   = $_FILES['image']['name'];
  $image_ext    = strtolower(end(explode('.', $image_name)));

  upload_page_image($image_temp, $image_ext, $post_id);

现在,在 android 中,我遇到了 Base64 编码字符串:

  params.put("image",imageToString(bitmap));

然后,我将它带到我的 PHP 文件中,如下所示:

  <$php
  .... 
  $image = base64_decode(_POST['image']);
  ...

但是现在,我如何将其制动到$_FILES['image']['temp_name']$_FILES['image']['name']?我经历的所有其他示例都使用file_put_contents。我需要它是move_uploaded_files

感谢任何帮助。 谢谢

【问题讨论】:

    标签: php android image image-uploading


    【解决方案1】:

    好吧,那是我经历过的最困难的事情。问题是当我使用 php 函数file_put_contents() 时,它首先让 android app 端的一切变得简单,只需将位图转换为 Base64 并将其传递给 php 文件。但是...... 在这个过程中丢失了一件非常重要的事情......那就是 $exif['Orientation'] 在使用 exif_read_data 时。这个参数已经没有了。

    现在,当回到 android 应用程序并尝试在将文件发送到 php 之前获取图像方向时,标准函数如下:

    ExifInterface exifReader = new ExifInterface(mFilePath);
    exifReader.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);
    

    .. 或类似的不起作用。它总是返回 0。总是

    解决方案来自使用 Glide https://github.com/bumptech/glide 库并使用以下方法获取当前图像方向:

    InputStream is = getActivity().getContentResolver().openInputStream(originalUri);
    int orientation = new ImageHeaderParser(is).getOrientation();
    

    获得方向后,将图像作为 Base64 和方向编号传递,然后处理上传并进行方向更改等。

    【讨论】:

      【解决方案2】:

      您必须使用 Multipart Form Data 提交图像才能在 php 中使用 move_uploaded_files

      【讨论】:

        【解决方案3】:

        【讨论】:

          猜你喜欢
          • 2013-07-22
          • 2014-12-22
          • 2013-05-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多