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!