Encode image to Base 64 and then send it. PHP

0

Very good day, I'm new to SO, and it's fascinating. I have a small problem I'm new to PHP and I still do not quite master it. Make a small code that receives me from an application on Android some data. How are they:

<?php
if($_SERVER['REQUEST_METHOD'] == 'POST'){
    //$idmascota = $_POST['idmascota'];
    $nombre = $_POST['nombre'];
    $fechanacimiento = $_POST['fechanacimiento'];
    $estatura = $_POST['estatura'];
    $sexo = $_POST['sexo'];
    $peso = $_POST['peso'];
    $fotografia = $_POST['fotografia'];
    //$iddueno = $_POST['iddueno'];
    $iddueno = 'randomtext';

    require_once('dbConnect.php');
    $sql = "SELECT idmascota FROM mascota ORDER BY idmascota ASC";

    $res = mysqli_query($con, $sql);

    $id = 1;

    while ($row = mysqli_fetch_array($res)) {
        $id = $row['idmascota'];
    }

    $path = "uploads/$id.png";

    $actualpath = "http://192.168.0.5/Prueba/$path";

    $sql = "INSERT INTO mascota (nombre, fechanacimiento, estatura, sexo, peso, fotografia, iddueno) VALUES ('$nombre','$fechanacimiento','$estatura','$sexo','$peso', '$actualpath', '$iddueno')";
    if(mysqli_query($con, $sql)){
        file_put_contents($path, $fotografia) or die("Error en la escritura");
        echo "Subio imagen correctamente";
    }
    mysqli_close($con);
} else {
    echo "Error";
}

Where I receive data from a dog and the field 'photography' is a text in Base64 that previously became the Android application from an image. And in the field photography of the BDD I keep the address of the server with the 'id' that auto increases in the PHP code and in the BDD to later load it as if it were a "src". But when wanting to implement it on the Web, with an 'input type file' it is obvious that it is not in Base64 and when applying the "base64_decode" it generates a corrupt file.

Try the 'file_put_content' and remove the 'base64_decode' so that the image is saved, but it seems not to.

I also attach the code of 'dbConnect.php'.

<?php 
define('HOST', 'localhost');
define('USER', 'root');
define('PASS', '');
define('DB', 'android_api');
$con = mysqli_connect(HOST, USER, PASS, DB) or die ('No se puede conectar a la base de datos');

And the form that sends the data, is irrelevant, but I'm desperate, chance and there's the error.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="upload.php" method="post">
    <input type="text" name="nombre">
    <input type="text" name="fechanacimiento">
    <input type="text" name="estatura">
    <input type="text" name="sexo">
    <input type="text" name="peso">
    <input type="file" name="fotografia">
    <input type="submit" name="submit">
</form>
</body>
</html>

I hope you can help me, and thank you:).

    
asked by BrandonRG 09.07.2017 в 21:10
source

0 answers