upload images + web service php

Asked

Viewed 753 times

1

Good people, I’m a little behind on my CBT because I can’t upload the image. What I have hj worked and take the photo, save in the gang sqlite the path and latitude and longitude, however I have a screen in my app that I must send an image with a registration, the registration is ok only missing the image. The registration data are sent to a web service in PHP, but I do not know how to send the image, and I have to select the image gallery and send,returning its path and save in the database along with the other data sent form. If anyone can help me I’d appreciate it...

WEB SERVICE CODE

 <?php
 if($_SERVER["REQUEST_METHOD"]=="POST"){
require 'connect.php';
inReg();
 }
 function inReg() {

    global $connect;

    $id_usuario = $_POST["id_usuario"];
    $tipo_atividade = $_POST["tipo_atividade"];
    $tipo_local= $_POST["tipo_local"];
    $quantidade = $_POST["quantidade"];


    $query = " Insert into registro_animal(id_usuario, id_tipo_atividade,id_tipo_local,quantidade) values ('$id_usuario','$tipo_atividade','$tipo_local','$quantidade')";
        mysqli_query($connect, $query) or die (mysqli_error($connect));
    mysqli_close($connect);
}

 ?>
  • I know how to do this, but I’m on duty and I can’t help you now! =)

  • No stress, the time you can help me will be great, because I’m late on this issue. I’m on hold!

  • I put an answer and a link at the end that will help you a lot. Practically ready for you, just have to have the tricks to adapt to your code.

1 answer

1

Receiving image

At first it is necessary to do a classic and quick upload of only one file. One of the attributes that we can not forget to do upload, is what arrow the enctype to our form. One of the ways is to use method="POST" and enctype="multipart/form-data".

POST METHOD

This feature allows you to upload text and binary files. With the PHP file authentication and manipulation functions, you has complete control of who can upload and what should be done with file after upload is complete.

PHP

if(isset($_FILES['fileUpload'])){
      date_default_timezone_set("Brazil/East"); // definindo timezone padrão

      $ext = strtolower(substr($_FILES['fileUpload']['name'],-4)); //pegando extensão do arquivo
      $new_name = date("Y.m.d-H.i.s") . $ext; //definindo um novo nome para o arquivo
      $dir = 'uploads/'; //diretório de destindo do arquivo

      move_uploaded_file($_FILES['fileUpload']['tmp_name'], $dir.$new_name); //Fazer upload do arquivo
   } 

As a "good practice" is defined a variable with the year, month, day, hour, minute and second, concatenating with the image extension. To finally upload the file, the function is used move_uploaded_file, passing two parameters, first the temporary name of the image stored by the server, and the new name of the file, already with default directory.

Sending picture

Already in sending by Android, one of the ways is you use:

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(Config.FILE_UPLOAD_URL);

build.Gradle

android {
    useLibrary 'org.apache.http.legacy'
}

For more details, you can access this article Android Uploading Camera Image, Video to Server with Progress Bar that will help you a lot.

  • Thanks for the answer, I’m trying here, anything I ask again !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.