0

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!!

  • Hello, have you tried this ? https://stackoverflow.com/questions/22567306/python-requests-file-upload – Maxime Nov 25 '19 at 13:38
  • I've solved it, turns out I have a linux system where I'm sending the file to a windows sistem, in Linux you can name files like this: 12:11:10.JPG, but in windows you can't use ":" to name files, so, the code was working perfectly it was all about namig files, but the error it would throw is pretty much useless, so, hope it helps someone in the future!! – Alejandro Flores Nov 26 '19 at 00:35

0 Answers0