Problem uploading file to bbdd with sql

0

I can not get this code to work for uploading the file to the bbdd.

If it is saved in the directory but not in the bbdd.

Code:

<?php

        $nombre_archivo = $_FILES['archivo'] ['name'];
        $tipo_archivo = $_FILES['archivo'] ['type'];
        $tamano_archivo = $_FILES['archivo'] ['size'];

        if($tamano_archivo < 2048000){

                //Ruta carpeta imagenes
                $carpeta_destino = $_SERVER['DOCUMENT_ROOT'] . '/intranet/uploads/';
                //Mover imagen del temporal al definitivo
                move_uploaded_file($_FILES['archivo'] ['tmp_name'],$carpeta_destino.$nombre_archivo);

        } else {

            echo 'Has excedido el tamaño de la imagen';
        }

        require('connect.php');

        $connection=mysqli_connect($db_host, $db_user, $db_pass);

        if(mysqli_connect_errno()){
            echo "Error de conexión";
            exit();
        }

        mysqli_select_db($connection, $db_name) or die ("No se encuentra la BBDD");
        mysqli_set_charset($connection, "utf8");

        $archivo_objetivo=fopen($carpeta_destino.$nombre_archivo, "r");
        $contenido=fread($archivo_objetivo, $tamano_archivo);
        $contenido=addslashes($contenido);
        fclose($archivo_objetivo);

        $query="INSERT INTO files (id, nombre, tipo, arv) VALUES ('', '$nombre_archivo', '$tipo_archivo', '$contenido')";
        $result=mysqli_query($connection, $query);

        if(mysqli_affected_rows($connection)>0) {
            echo 'Se ha insertado el archivo con exito';
        } else {
            echo 'El archivo no se ha insertado';
        }
    ?>
    
asked by Xavi 24.07.2017 в 12:32
source

1 answer

0

Some more information is needed. The first thing I recommend is not to save a file in the database, they are not designed for it even if they allow it, instead it saves a URI.

On the other hand, if you insist, the destination field must be of type BLOB. Share the structure of the database to help you.

Greetings.

    
answered by 24.07.2017 в 12:46