Download file from mysql server

1

Good companions, I have a problem in my code and I can not fix it ...

I'm trying to upload files and download them with php, the problem is that when I give it to download it leaves the page blank and does not download anything ..

this is my code when uploading the file:

And that is my code to download the file:

include('conexion.php');

$id = $_GET['id']; 
$qry = "SELECT tipo, contenido, archivo FROM archivos WHERE idarchivos=".$id."";
$res = mysqli_query($link, $qry);
$tipo = mysql_result($res, 0, "tipo");
$contenido = mysql_result($res, 0, "contenido");
$nombre = mysql_result($res, 0, "archivo");

header("Content-disposition: attachment; filename=".$nombre."");
header("Content-type: ".$tipo."");

echo $contenido;
    
asked by Jesús 19.04.2017 в 21:52
source

2 answers

1

From the outset I suggest you avoid saving the contents of the file in the database, since in addition to having a negative impact on performance and space utilization, special code is required to read and write data when the size exceeds certain limit (32KB if I remember correctly).

The best practice is to save only a reference or file path in the database so that at the time the download is requested, read from the database only the path (or the data needed to build it) the path) and then in PHP directly open the file for download.

    
answered by 19.04.2017 в 22:06
1

Here I leave the change, so you can call the file directly without having to print the content:

include('conexion.php');

$id = $_GET['id']; 
$qry = "SELECT tipo, contenido, archivo FROM archivos WHERE idarchivos=".$id."";
$res = mysqli_query($link, $qry);
$nombre = mysql_result($res, 0, "archivo");

header("Location: subir_archivo/archivos".$nombre.);
  

Note: the path where the physical file is verified because I can not see it well. The name of the reference has to be complete with the extension can not be separated.

    
answered by 19.04.2017 в 22:52