Insert files (images, videos, documents, pdf, docs, excel, impress ...) in a DB with php mysql with BLOB

0

I'm trying to make a form that, insert an image file in the DB. But do not insert anything, because it will be?

HTML:

    <form method="POST" action="" id="form_chat_do" enctype="multipart/form-data">
    <table id="botones_chat_do">
        <tr><td><textarea placeholder="Escribe tu mensaje" name="mensaje_text"></textarea><input type="text" id="validar_chat"></td></tr>
        <tr><td>
            <div id="div_file">
                <i id="texto_archivo" class="fa fa-cloud-upload"></i>
                <input type="file" name="archivo" id="añadir_archivo">
            </div>
            </td>
        </tr>
        <tr><td><button id="enviar"><i class="fa fa-send"></i></button></td></tr>
    </table>
</form>

PHP:

<?php
$conexion = new mysqli("","","","");
if (!$conexion) {
    die("Error al conectar con la DB: ".$conexion->connect_error);
}
date_default_timezone_set('Europe/Madrid');
$fecha = date("d/m/Y");
$hora = date("G:i");
$mensaje = $_POST['mensaje_text'];
// Este es el archivo temporal:
$tipo = $_FILES['archivo']['type'];
$nombre = $_FILES['archivo']['name'];
$tamaño = $_FILES['archivo']['size'];
$imagen = $_FILES['archivo']['tmp_name'];
$insertar = "INSERT INTO table1 (clase,nombre_archivo,tipo_archivo,tamaño_archivo,mensaje,fecha,hora) VALUES ('1A','".$nombre."','".$tipo."','".$tamaño."','".$mensaje."','".$fecha."','".$hora."')";
echo mysqli_query($conexion,$insertar);
$conexion->close();
?>

SQL:

CREATE TABLE IF NOT EXISTS 'table1' (
'id' int(11) NOT NULL AUTO_INCREMENT,
'clase' varchar(3) NOT NULL,
'nombre_archivo' varchar(255) NOT NULL,
'tipo_archivo' varchar(50) NOT NULL,
'data_archivo' blob NOT NULL,
'tamaño_archivo' bigint(20) NOT NULL,
'mensaje' varchar(11000) NOT NULL,
'fecha' varchar(10) NOT NULL,
'hora' varchar(5) NOT NULL,
PRIMARY KEY ('id')
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

Thank you.

    
asked by cat_12 16.09.2018 в 16:59
source

1 answer

0

As such you can not put a file in the BD, it is a bad practice, however if you really want to enter it you can pass it to base 64 in PHP with base64_encode() which allows you to enter it as a String to your BD, to return it to get you just have to decode the image base64_decode()

    
answered by 16.09.2018 в 17:09