Upload an image to mysql with php without the need for a form

0

Good to everyone as they are ..! Well my question is: can you upload an image to MySQL without having to make a form?

What I want to do is that I automatically upload the images that are in a folder to the database, because I'm really doing an application where I capture the computer screen and I want to take the screenshots and be able to see those photos in another team, but I want you to save the capture in mysql or find a way that the other team can see them.

This is the little code I have but only saves the route.

<?php
$i=0;
while ($i<5) {
    require ('config.php');
    $i++;
    $Nombre = "Reporte".$i;
    $guardado = "../img/".$Nombre.".jpeg";
    $img = imagegrabscreen();
    $destino_1 = imagejpeg($img,$guardado);    

            $conexion = mysqli_connect($db_host,$db_user,$db_pass);
            if (mysqli_connect_errno()) {
                echo "Fallo al conectar con la Base de Datos.";

                exit();

            }

            mysqli_select_db($conexion, $db_bd) or die ("No se encontro la Base de Datos");

            mysqli_set_charset($conexion, "utf8");
    $guardar= "INSERT INTO img (ID, Nombre, Lugar) VALUES (0, '$Nombre', '$guardado') ";

        $resultado=mysqli_query($conexion,$guardar);

        mysqli_close($conexion);
    sleep(15);
}
?>
    
asked by Hayate GS 27.08.2018 в 08:52
source

2 answers

1

Assuming you receive $ _POST somewhere you should do this

//esto es par aque no te lo guarde con la tura C:\fakepath\nombredelfichero

$_POST['tu_fichero'] = basename($_POST['tu_fichero']);

//no sé como tendras las cosas pero ese metodo es para guardar en base de datos.
guardar_datos($_POST, $conn);

to make a kind of paripé you should have in html this, the rest of the form should be sent to a .PHP apart from the type of text is what will be stored in the database, and obviously the type File, is the that will move to where you tell it.

<div class="file-field input-field col s11">
     <div class="input-append">
          <input id="photoCover1"  value="" name="fichero"class="form-control" type="text" onclick="$('input[id=fichero1]').click();">
     </div>
     <label for="fichero1">Fichero</label>
</div>

what is below is what is sent asynchronously and moves to the folders

   <div class="hide">
        <form name="fichero1" method="post" enctype="multipart/form-data" action="#">
            <input id="fichero1" name="fichero" type="file">
        </form> 
    </div>

this would be the javacript code     

$('input[id=fichero1]').change(function() {
    $('#photoCover1').val($(this).val());
    subir_fichero();
});


function subir_fichero(){
    $.ajax({
        url: '.file_upload.php',
        type: 'POST',
        data: new FormData($('form')[1]),
        cache: false,
        contentType: false,
        processData: false, 

        success: function(response){
            //$("#new_prod_card").html(response);


        }
    });
}

</script>

once the file is sent asynchronously the .file_upload picks it up and sends it to this function

if ($_FILES['fichero']['name']!="")
{

    subir_fichero($_FILES);

}

this function contains this

function subir_fichero($fichero){

    $PATH = PATH_MIS_IMAGENES;

    move_uploaded_file($fichero['fichero']['tmp_name'], $PATH.$fichero['fichero']['name']);
    //var_dump($PATH.$fichero['fichero']['name']);

}

I do not know if it helps you but I've been working with this, I hope so, what should be clear is that one thing is to store something in a database and another to interact with files.

a greeting

    
answered by 28.08.2018 / 10:58
source
0

You should send them as binary files (in base64, for example). There are libraries that allow encodings in a very simple way.

Once you have encoded the file (s), you would have to save them in a field for objects, such as BLOB, depending on the database engine you are using.

    
answered by 27.08.2018 в 13:25