如何将图像从Android应用发送到Ftp服务器?

时间:2019-06-19 14:19:55

标签: php android flutter ftp

我正在尝试通过使用php ftp_put()的http请求将Flutter Android应用中的图像发送到ftp服务器,但是我得到了:

ftp_put(File: '/storage/emulated/0/Android/data/br.com.dannilosn.maisservico/files/Pictures/scaled_f11abeeb-08b2-4ef3-bbaf-978ce01066f83153399647709822912.jpg'): failed to open stream: No such file or directory in /mnt/Backup/Git/mais-servicos-server/servidor/ftp/funcoes/enviar_arquivo.php

路径为: "/storage/emulated/0/Android/data/br.com.dannilosn.maisservico/files/Pictures/scaled_db8a8040-3c45-453a-946b-701bed9815ec7965385997102163330.jpg".

Ftp代码:

class Ftp{
  Ftp(ip){
    this._ip = ip;
    _url = _HOST + _ip +_PATH;
  }

  static const String _HOST = "http://";
  static const String _PATH = "/servidor/ftp/funcoes/";
  String _ip;
  String _url;

  static const String _ENVIAR_ARQUIVO = "enviar_arquivo.php";

  Future<dynamic> enviarArquivo(String origem) async{
    var response = await http.get(
      _url + _ENVIAR_ARQUIVO +
      "?origem=$origem"
    );
    if(response.statusCode == 200){
      var res = response.body;//convert.jsonDecode(response.body);
      print("Enviar arquivo: ${res.toString()}");
      return res;
    }
  }

}

enviar_arquivo.php:

<?php

$ftp_host = "*******";
$ftp_user = "*******";
$ftp_pass = "*******";

$ftp_stream = ftp_connect($ftp_host);

if(!ftp_login($ftp_stream, $ftp_user, $ftp_pass)){
    echo "Não conseguiu se conectar";
    exit();
}

$destino = "/servidor/ftp/arquivos/teste.jpg";
$origem = $_GET["origem"];

ftp_pasv($ftp_stream, true);

$resposta = ftp_put($ftp_stream, $destino, $origem, FTP_BINARY);
if($resposta){
    echo "OK";
}else{
    echo "Arquivo não enviado";
}

ftp_close($ftp_stream);

?>

如果我使用文件管理器搜索,则存在文件。

我应该做些其他的事情来在Android上获取文件还是在php中呢?

1 个答案:

答案 0 :(得分:0)

您的HTTP / PHP服务器无法通过其路径(这将是一个严重的安全漏洞)来访问客户端(Android)上的文件。

您必须将实际内容发送到HTTP / PHP。

有关类似问题,请参见:
Upload a local file from application via web server with PHP code to FTP server