-1

I'm trying to upload image from my apache cordova app to my Php server. I have an Android app that use the same service from the serve tu upload file and works perfect.

When I'm trying to upload file from my apache-cordova I think the server is not receiving the file correctly.

With my android app I send a file with multipart/form-data

This is what my apache-cordova do:

var opciones = {
    fileKey: "file",
    fileName: "image",
    chunkedMode: false,
    mimeType: "multipart/form-data",
    params : {'fileName': "image"}
};


//guardar imagen cargada
    function guardarImagen(urlImagen, opciones) {
        debugger;
        console.log("la ruta de la imagen es:"+urlImagen);
        return $cordovaFileTransfer.upload(cargarImagenUrl, urlImagen, opciones)
            .then(function(response) {
                respuesta = response;
                debugger;
                return respuesta;
        });

    }

My PHP:

 $image = new \Bulletproof\Image($_FILES);
//$idComplejo= $_POST['idComplejo'];
  //$idComplejo= $decoded_token->idComplejo;
  $idComplejo= "21";
  $image->setName("1");
  $image->setLocation(__DIR__ . "/../imagenes/complejo----------".$idComplejo);

  //$image->setLocation("imagenes/complejo-".$idComplejo);
  $image->setSize(1, 1148576);
  $image->setDimension(10000, 10000);
  $error = "Not uploaded".$image;

  if($image['image']){
    $upload = $image->upload();
    $directorio = "https://www.myhosting.com/imagenes/complejo-".$idComplejo."/1.".$image->getMime();

    if($upload){
      $result = $db->query("SELECT rutaImagen FROM imagen WHERE idComplejo = :idComplejo AND nroImagen = 1",
          array("idComplejo" => $idComplejo));

      if(count($result) == 0){
        $error.="no se encontro imagen para ese complejo, se procede a guardar";
        // Nunca se cargo la imagen nro 1 y se procede a guardarla en la BD
        $result = $db->query("INSERT INTO imagen (idComplejo, nroImagen, esPrincipal, rutaImagen) VALUES (:idComplejo, 1, 0, :directorio)",
        array("idComplejo" => $idComplejo, "directorio" => $directorio));

        $result= mysql_query("") or die(mysql_error());
        // Imprimimos
        $response["success"] = 1;
        $response["message"] = "La imagen se ha guardado correctamente.";
        echo ($response);
      }
      // verificmos que devuelva datos
      else if (count($result) > 0) {

        $result = $db->query("UPDATE imagen SET rutaImagen = :directorio WHERE idComplejo = :idComplejo AND nroImagen = 1",
        array("idComplejo" => $idComplejo, "directorio" => $directorio));
        echo json_encode("La imagen se ha actualizado con éxito.");
      }

    }
    else{
      $error .= "La imagen no se guardo debido a:" . $image["error"];
    }

  }

  file_put_contents('errors.txt', $error, FILE_APPEND);
  file_put_contents('errors.txt', error_get_last(), FILE_APPEND);
?>

the line $error = "Not uploaded".$image; not catching anything

Like I said, I think the server is not receiving the file correctly.

I'm getting correctly the file that I want to upload because the console before send the file show:

"la ruta de la imagen es:file:///data/user/0/com.myapp.example/files/1483828947649.jpg"

What's I'm doing wrong?

Thanks!

Faustino Gagneten
  • 2,564
  • 2
  • 28
  • 54

1 Answers1

0

The error is in this innocent looking line:

$error = "Not uploaded"+$image;

The + operator tries to convert $image to int. You could just try

$error = "Not uploaded" . $image;

See also: how to convert object into string in php

Community
  • 1
  • 1
Turbo J
  • 7,563
  • 1
  • 23
  • 43
  • haha, this is the problem when you are developping in multiples languages. Ok the error is gone, but not working. The $eror was for see what I'm receiving. Thanks for the mistake – Faustino Gagneten Jan 07 '17 at 23:02
  • The question was edited using the `.` instead of `+` and they did not mark it as an additional edit. This no longer applies as an answer; just saying. Edited comment: @FaustinoGagneten and you did not mark your edit "as" an additional edit, stating that you made a mistake, and this person stands to get downvoted because of this. @ Turbo. If you do get a downvote, I can assure you that it would not have come from me. – Funk Forty Niner Jan 07 '17 at 23:54
  • Sorry, I did not take in account that. I've just add edit summary – Faustino Gagneten Jan 08 '17 at 00:16