Encode an image and pass it to a database

My host has turned off Mod_Security for the domain and that's ok but the BLOB is still not going to the database. The name is going to the current directory. If I hard code the URL to an image in the current directory it works fine.

What do you get in the error_log file?

How is your database structured?

When you say: "The name is going to the current directory." what does it mean? that the file is saved in the current directory or that only the name is saved in the current directory with size 0?

Yes the file is saved in the current directory with size 0.
dbase

if (file_put_contents(basename($_GET['imagen']),$file == TRUE)) {

The problem is this line is finding noting

Does this work for you, with your domain?

<?PHP
   $data = file_get_contents('php://input');
   if (!(file_put_contents($_GET['archivo'],$data) === FALSE))  {
       echo "Subido.";
   } else {
       echo "Error.";
   }
?>

borrar_sube2.aia (34.4 KB)

Yes it seems to be working but it's not finding the hard coded image gato.png and throwing an error. The most promising result I had yet though. Something to work on.

error.txt (418 Bytes)

How do you send a camera image to this. I have tried directory paths and keep getting this error

The file you are trying to create a path for will NOT be in assets, and that path will only be available when using companion app.

You are in file path creation hell. You need to read @Anke's guide about storage in AppInventor to understand how file paths should be constructed for the many variations: companion / compiled, assets /ASD...

photo2

Difficult to say with some certainty without seeing all the blocks, that said, everything looks to be correct, if gato.png is in the media folder. You may need a / before gato.png in the MakeFullPath block?

When you say "Yes it seems to be working but it's not finding the hard coded image cat.png", what do you mean? which cat.png has uploaded to the server correctly? So what does not finding the hard coded cat.png mean?

I'm not wasting any more time trying to do this. App Inventor can't send images to an external data base.

Yes it can, with correct setup at the server, the correct php file, and the correct blocks in AI2

Now I've caused my phone to lock up using this !

Again, you do not show, in their entirety, your relevant blocks....

Before, we uploaded a cat. Now, we are going to upload a dog.

borrar_sube3.aia (166.8 KB)

<?php
 // 1.- IDENTIFICACION nombre de la base, del usuario, clave y servidor
$db_host="localhost";
$db_name="id1975555_base"; // fake
$db_login="id1975555_juan"; // fake
$db_pswd="Contraseña"; // fake

// 2.- CONEXION A LA BASE DE DATOS
$link = new mysqli($db_host, $db_login, $db_pswd, $db_name);

// 3.- OBTENER ARCHIVO - GET FILE
  $file = file_get_contents('php://input');
   if (file_put_contents($_GET['archivo'],$file == TRUE))  {
   
		$nombre = $_GET['archivo'];
		$extension = pathinfo($nombre, PATHINFO_EXTENSION);
		// $extension = mime_content_type($nombre);
		
		// Archivo a MySQLi. BLOB. - File to MysQLi BLOB.
		$blob = mysqli_real_escape_string($link, $file);
		$resultado = mysqli_query($link, "INSERT INTO dibujos (imagen, extension, nombre) VALUES ('$blob', '$extension', '$nombre')");
		mysqli_close($link);	
		echo $nombre." uploaded.";
   } else {
	echo "Error";  
   }
?>

Here it is!

1 Like

Can you adjust this to store an image just taken with the camera ?

Does my example work for you? can you upload the perro.png file to your database?

I'm sure it works. It's not finding the directory. A small issue. Can you please change the above blocks to allow picking an image from your phone.