Android Example Upload image to server, PostMultipart and PHP

A small example like loading an image with PostMultipart and PHP, I have been useful, you can adapt it needs.

Code B4A
B4X:
Sub CargaImagen
    Dim j As HttpJob
    Dim img As String = "IMG_20178108230.jpg"
    j.Initialize("", Me)
    Dim mp As MultipartFileData
    mp.Initialize
    mp.Dir = File.DirRootExternal & "/Image/"
    mp.FileName = img
    mp.KeyName = "file"
    mp.ContentType = "image/jpg"
    j.PostMultipart("http://192.168.0.108/filephp/UploadImage.php", Null, Array(mp))
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Log(j.GetString)
        If img = j.GetString Then
            ToastMessageShow("Imagen Cargada.", False)
        Else
            ToastMessageShow("Error al cargar Imagen.", False)
        End If
    End If
    j.Release
End Sub

Code PHP
PHP:
<?php
/**
* Created by PhpStorm.
* User: rscheel
* Date: 10-08-2017
* Time: 8:26
*/
define('KB', 1024);
define('MB', 1048576);
define('GB', 1073741824);
define('TB', 1099511627776);

$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);

$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

// Compruebe si el archivo de imagen es una imagen real o una imagen falsa
$check = getimagesize($_FILES["file"]["tmp_name"]);
if($check !== false) {
    //echo "El archivo es una imagen - " . $check["mime"] . ".";
    $uploadOk = 1;
} else {
    //echo "El archivo no es una imagen.";
    $uploadOk = 0;
}

if($uploadOk) {
    // Comprueba si el archivo ya existe.
    if (file_exists($target_file)) {
        echo "Lo sentimos, el archivo ya existe.";
        $uploadOk = 0;
    }
    // Verificar el tamaño del archivo
    if ($_FILES["file"]["size"] > 1 * MB) {
        echo "Lo siento, su archivo es demasiado grande.";
        $uploadOk = 0;
    }
    // Permitir ciertos formatos de archivo
    if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
        && $imageFileType != "gif") {
        echo "Lo sentimos, sólo se permiten archivos JPG, JPEG, PNG y GIF.";
        $uploadOk = 0;
    }
    // Compruebe si $uploadOk está establecido en 0 por un error
    if ($uploadOk == 0) {
        echo "Lo sentimos, su archivo no se ha cargado.";

    // Si esta bien, trata de guardar archivo
    } else {
        if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
            echo basename($_FILES["file"]["name"]);
        } else {
            echo "Error!!!";
        }
    }
} else {
    echo "El archivo no es una imagen.";
}
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
Please note that with this php you only can upload only ONE file with the postmultipart as you are statically using the Keyname "file".
You can for sure upload multiple files with the same Keyname but your script will only handle one of them.
 
Last edited:

rscheel

Well-Known Member
Licensed User
Longtime User
Please note that with this php you only can upload only ONE file with the postmultipart as you are statically using the Keyname "file".
You can for sure upload multiple files with the same Keyname but your script will only handle one of them.

Exactly, just do it thinking of uploading a file to see more than one, was what I needed, and if you want to upload more to see you have to adapt the code.
 

Roberto P.

Well-Known Member
Licensed User
Longtime User
thanks for sharing.

It would be an interesting example with a server developed with B4J to have a B4X solution.

I hope someone does.
 
Top