uploading images + web service php

1

Good people, I'm a little late in my tcc because of not being able to upload the image. What I have hj worked and take the photo, save in the flock sqlite the path and latitude and longitude, however I have a screen in my app that I should send a picture with a register, the registration is ok only missing the image. The registration data is sent to a web service in PHP, but I do not know how to send the image, and I have to select the image from the gallery and send it, returning the path of it and save it in the database along with the other data. form submitted. If anyone can give me a help then thank you ...

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);
}

 ?>
    
asked by anonymous 25.08.2016 / 20:04

1 answer

1

Receiving image

At first it is necessary to make a classic and fast upload of just one file. One of the attributes that we can not forget to do upload , is what arrow enctype to our form. One way 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 authentication and file manipulation functions, you   have full control of who can upload and what should be   made with the file after the 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" a variable is defined with the year, month, day, hour, minute and second, concatenating with the extension of the image. To finally upload the file, the move_uploaded_file function is used, passing two parameters, first the temporary name of the image stored by the server, and the new file name, already with a default directory.

Uploading image

While sending from Android, one of the ways you can 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.

    
25.08.2016 / 20:45