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
phpsubirK1

<?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";
?>

p327C_subir_archivo_textoK.aia (324.4 KB)

https://kio4.000webhostapp.com/dibujo.jpg

4 Likes

Now with different name of image:
phpsubirK2

p327C_subir_archivo_textoK_2.aia (324.5 KB)

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

$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";
?>
2 Likes

Cam Take Picture and upload to server.

The same code PHP from the previous post

p327C_subir_archivo_textoK_Cam.aia (325.2 KB)

The photos usually have a size of 2MB … 4MB, it will take a while to upload to the server.

3 Likes

The files will be in the same directory that the php file is located?

In my examples, the PHP code and the file we have uploaded are in the same directory, but you can change the PHP code so that it can be saved in another directory.

2 Likes

Save the file in a different directory than the PHP code:

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

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

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

echo "Creo que ha subido";
?>
1 Like

Or pass the directory you want as a parameter and in the script to check if the folder exists or not; if it does not exist, it is created;

Hello,
Since the new version of Android, the file to string does not work anymore wheras it worked very well before.
Can you help me ?
Vincent

@vinc U mean in android 11, Base64 does not work for files?

1 Like

Yes I mean with Android 11
But Juananton1991 allready replied me through an other way, he is going to change its code.
Thank you Juan.
Vincent

Try version KIO4_Base64v10.aix

Edited:

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

  • 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