I'm trying to send a photo from a raspberry pi 3 to a php service, in phyton i'm using requests library but it doesn't seem to work. From my raspberry i'm using this code
def send_data_to_server(image_path, id):
image_filename = os.path.basename(image_path)
multipart_form_data = {
'image': (image_filename, open(image_path, 'rb')),
'id': (id),
}
response = requests.post('http://192.168.0.7:8080/photos/cargar.php',
files=multipart_form_data)
print(multipart_form_data)
print(response)
In my php server i'm using this other code:
<?php
$servidor="localhost:3306";
$root="ale";
$BaseDatos="fotoa";
$password="gatoloco2";
$conexion = new mysqli($servidor,$root,$password,$BaseDatos);
if($conexion->connect_errno){
echo "Sitio no disponible";
}else{
echo "conexión exitosa";
}
if($_SERVER['REQUEST_METHOD']=='POST'){
$target = "images/".basename($_FILES['image']['name']);
$image = $_FILES['image']['name'];
$guessID = $_POST['id'];
$ID =implode('.',$guessID);
$consulta = "INSERT INTO photo (url, guessID) VALUES ('$image', '$ID')";
mysqli_query($conexion,$consulta) or die (mysqli_error($conexion));
echo $guessID;
if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
$msg = "Archivo correctamente agregado";
} else {
$msg = "Hubo un problema, reintente";
}
echo $msg;
}
?>
Also, I can't get the ID field The frustrating thing is if i do it in local using the following form
<form action="" method="post" enctype="multipart/form-data">
Name: <input type="text" name="id">
Image: <input type="file" name="image">
<input type="submit" value="submit">
</form>
it works like a charm!!!!
I really don't know what to do :( pls help!!