How to force the download of a file, which is stored in the database

4

I have a system where, documents from the database are listed, where the user has the option to view or download the file. I want to know how I can retrieve this file from the database and force it to be downloaded. This file is in .pdf

This is the code I have

header("Content-type: application/pdf");

//pega o arquivo 
$sql_select_file = "SELECT * FROM malote_arq_2014 WHERE reg=" . $reg . "";
$query_select_file = mysqli_query($conn, $sql_select_file);
$result = mysqli_fetch_array($query_select_file);

header("Content-Disposition: attachment; filename=".$arquivo);

But it returns me the following message:

Header may not contain more than one header, new line detected in

    
asked by anonymous 05.09.2014 / 19:57

1 answer

3

Probably $arquivo is coming with special characters at the end.

Try using the trim function to clear these characters:

header("Content-Disposition: attachment; filename=".trim( $arquivo) );

This solution is best as a means of only identifying the problem, since ideally the file name would already be originally captured without any unnecessary characters.

Also try commenting on these lines:

// header("Content-type: application/pdf");
...
// header("Content-Disposition: attachment; filename=".$arquivo);

and put a print_r( $arquivo ); at the end to identify other mistakes.

  

Make sure you're not putting the file content in place of the file name .

    
05.09.2014 / 20:11