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

Asked

Viewed 822 times

4

I have a system where, are listed documents from the database, where the user has the option to view or download the file. I want to know how to rescue this file from the bank and force the download of it. This file is on .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);

Only he returns me the following message:

Header may not contain more than a single header, new line Detected in

  • Have you seen this article? http://blog.thiagobelem.net/forcando-download-de-arquivos-com-php/

  • Yes, and I already tested that script, but it keeps returning me the same message I mentioned above

  • 1

    @Mathdesigner47 since you were able to solve it and it was a very specific error, I would suggest deleting the question in this case.

  • @Bacco, I was really confusing the file name with the content. In the 'header' I was passing the contents of the file, I tried to put any name, and it worked. Problem solved!

  • @Mathdesigner47 great. I still think it would be the case to delete the question, but see what you think best! The important thing is that solved.

1 answer

3


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

Experiment using the function trim to clear these characters:

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

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

Also try to comment on these lines:

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

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

Also make sure you are not putting the contents of the archive in place of file name.

  • keeps giving the same error message

  • is that because I omit the file size, is the reason for this error?

  • @Mathdesigner47 try the suggestion I put in to see what’s in $file. I suspect there’s something wrong there. If not, you might have problems elsewhere in the code. It would be good for you to put the full code, or at least the part that creates the variable $arquivo.

  • I used your suggestion, it shows me a lot of characters, which are illegible. And I think it’s not a mistake in the code, because I use this same code to display the file, in the browser

  • 1

    got, that’s exactly what you said, was passing to the header the contents of the file. I tried to put the name, and it worked

Browser other questions tagged

You are not signed in. Login or sign up in order to post.