Upload file to server hosting by PHP. Encode file to Base 64 extension

Hello friends,

there are several ways to upload a file to our server: FTP, Postfile,… in this example we are going to convert the file into text using a Base 64 extension and upload that text using PHP.

You can download the extension com.KIO4_Base64.aix of:
http://kio4.com/appinventor/277_extension_imagen_string.htm

In this example I will use 000webhost server

phpsubir

<?php
// Juan A. Villalpando
// kio4.com
// https://kio4.000webhostapp.com/dibujo.jpg

$datos=$_POST;
$contenido=$datos['contenido'];

$plainText = base64_decode(str_replace(array('-', '_',' ','\n'), array('+', '/','+',' '), $contenido));
$ifp = fopen( "dibujo.jpg", "wb" );
fwrite( $ifp,  $plainText );
fclose( $ifp );

echo "Creo que ha subido";
?>

Podremos ver la imagen:
https://kio4.000webhostapp.com/dibujo.jpg

p327C_subir_archivo_texto.aia (324.4 KB)

Regards,
Juan A. Villalpando.

1 Like

With this code you can set the name of the file.

phpsubir2

<?php
// Juan A. Villalpando
// kio4.com
// https://kio4.000webhostapp.com/name_file.abc

$datos=$_POST;
$contenido=$datos['contenido'];
$nombre=$datos['nombre'];

$plainText = base64_decode(str_replace(array('-', '_',' ','\n'), array('+', '/','+',' '), $contenido));
$ifp = fopen( $nombre, "wb" );
fwrite( $ifp,  $plainText );
fclose( $ifp );

echo "Creo que ha subido";
?>

Another example:

The Camera takes picture and upload to the server.

thanks both! taifun for redirect me to good tutorial and Juan Antonio you opened my eyes on split list item list :slight_smile:
i solved in this way:


blocks (5)

Hello all,
Since the new version of Android, the encode of Base64 doesn't work anymore.
I do not succeed to get a string anymore whereas it worked very well.
Can you help me ?
Vincent

I have modified the code, you can check if the new extension works: KIO4_Base64v10.aix, :

Edit:

  • If you are going to use this extension in Android 10 or higher, you must use the blocks that contain "ASD", the BASE directory is:

/storage/emulated/0/Android/data/'namepackage'/files

that is, if in the CreateDirectoryASD block you write: /my_directory, it will be created in:
/storage/emulated/0/Android/data/'namepackage'/files/my_directory

  • If you are going to use this extension in Android 9 or lower, you can use the blocks that contain "ASD" as I have indicated above, you can also use the blocks that do not contain "ASD", in this case the BASE directory is:

/mnt/sdcard

that is, if in the CreateDirectory block you write: /my_directory, it will be created in:
/mnt/sdcard/my_directory

  • This extension also contains the GetApi and GetSdkCodeName blocks, with these blocks you can check the version of Android that the device has installed.

http://kio4.com/appinventor/277_extension_imagen_string.htm

2 Likes

Tested in companion on Android 10 (genymotion emulator) and Android 11 (real device Google Pixel 4a)

This works OK

image

For a file in the assets / media folder, this works OK

image

Thank you Juan for your good work :slight_smile:

I note the extension for 10+ doesn't overwrite the earlier extension. Can both extensions be used at the same time, to handle different android versions ?

Hi @TIMAI2, thanks for testing the extension, I have taken your idea and joined the two versions of this extension, I have edited the previous post.

Good stuff. Thank you Juan :smiley:

Hi Juan

Using the extension today to encode an image from assets (on Android 10). This works:

image

but the tooltip says to use two slashes // - which doesn't work.

Which one is correct ?

Yes, only for Android < 10.
@Juan_Antonio

Does this app installed on Android 10 and 11 work?

borrar_test_base64.aia (79.3 KB)

[edited: //perro.png by perro.png]

Do we need to compile ?

yes, installed in Android 10 or 11.

Tested apk Android 10 and 11. Works OK. (Couldn't see if the directory intoASD was created in Android 11 due to Google lockdown, but created in Android 10, but no errors).

I guess we just need an if else block if we are working in development mode to access assets, in the MIT companion directory.

Thanks @TIMAI2 for testing it on Android10-11.

With double slash //gato.png works in development mode and installed.

- Image in asset to Base 64 and upload to web.

p327C_subir_archivo_imagen_Base64_ASD.aia (1.7 MB)

  • This example:

  • Converts an asset image to a Base 64 string, with block "FileToStringASD"

  • Show that string in a Label.

  • Convert that string to an image called: "/this_is_a_cat.png", save it in ASD, and show it in Image2

  • With block "PostText", sends string to web.

  • A PHP file converts that string to image: dibujo.png in web server.

  • You can look that image in a web page.
    (This web page always shows the image in a size of 40x40)

PHP code:

<?php
// Juan A. Villalpando
// kio4.com

$datos=$_POST;
$contenido=$datos['contenido'];

$plainText = base64_decode(str_replace(array('-', '_',' ','\n'), array('+', '/','+',' '), $contenido));
$ifp = fopen( "dibujo.png", "wb" );
fwrite( $ifp,  $plainText );
fclose( $ifp );

echo "Creo que ha subido";
?>

- PICK Image and convert to Base 64, upload to web. PHP.

p327C_subir_archivo_imagen_Base64_ASD_PICK.aia (1.7 MB)

  • We can explorer and PICK an image file with ActivityStarter:
    ActivityStarter1.Action = android.intent.action.PICK
    ActivityStarter1.DataType = image/*
  • We get ResultUri, example:
    content://com.mi.android.globalFileexplorer.myprovider/external_files/gato.png
  • Using the UriToPath block, we convert that uri to the path of the file:
    /storage/emulated/0/gato.png
  • We remove the base of that address to obtain /gato.png
  • That file to string: FileToString block.
  • That string to file in ASD: StringToFileASD.
  • With PostText upload string to web.
  • PHP file receives the string and converts it to the file dibujo.png
  • Show that image in a web page.

- PICK Image. Copy in ASD. Convert to Base 64. Upload to web. PHP.

  • Can anyone check if this app works installed on Android 11? Thanks.
    (use images smaller than 500 kB)

p327C_subir_archivo_imagen_Base64_ASD_PICK_CopyASD.aia (1.7 MB)

  • This app:

  • PICK an image. Convert Uri to Path.

  • Copy image to ASD with name: /image_in_ASD.png

  • Convert image ASD to string Base 64.

  • Upload string to webserver with PHP by Web component.

  • Show image in a web page in my hosting.

Hi Juan

Tested compiled on Android 11.

Picks image OK.

Doesn't show image2
Doesn't show web image