Get Bin File Stored in Mysql

Asked

Viewed 27 times

0

Long live!

I have a problem getting my PDF file that was stored in a Mysql BD. Whenever I download it, I cannot open it because it returns the message that may be damaged.

So I store:

$docs = new Documentos();

$assunto = filter_input(INPUT_POST,'assunto');
$origem = filter_input(INPUT_POST, 'origem');
$dataEntrada = filter_input(INPUT_POST, 'dataEntrada');

$fileName = $_FILES['ficheiros']['name'];
$ficheiro_temp = $_FILES['ficheiros']['tmp_name'];
$tamanho = $_FILES['ficheiros']['size'];
$tipo = $_FILES['ficheiros']['type'];

copy($ficheiro_temp, "../upload/$fileName");

$ficheiro = file_get_contents($ficheiro_temp);
$ficheiro = addslashes($ficheiro);


$docs->setAssunto($assunto);
$docs->setDataEntrada($dataEntrada);
$docs->setOrigem($origem);
$docs->setDocName($fileName);
$docs->setTipo($tipo);
$docs->setTamanho($tamanho);
$docs->setFicheiro($ficheiro);

$docs->armazenarFicheiros();

So I download it:

foreach ($docs->verFicheiro() as $f){
        $file = $f['ficheiro'];
        $size = $f['tamanho'];
        $type = $f['tipo'];
        $name = $f['nomedocumento'];
    }

    header("Content-length: {$size}");
    header("Content-type: {$type}");
    header('Content-Disposition: attachment; filename="'.$name.'"');

    fpassthru($file);

1 answer

0

There’s something strange in that download code. You’re using a foreach, but then you only upload the file once. Why are you doing a foreach?

Besides, what is this Documents? A class of yours? Or an external API? A lot of information is missing...

  • I use the foreach to scroll through my table to get the line specified in my WHERE clause ... Documents() is my class with the functions of storing and etc ... You said I send the file at once ... I was supposed to have something else?

  • @C-lioGarcia the problem is that you have a foreach, whose aim is to iterate for results, and then the rest of the code only runs once. If there’s only supposed to be one document, why the foreach?

  • @Bluntt, I used foreach as an example ... This code is only for testing ... Don’t tell me that foreach causes anything?

  • @C-lioGarcia the foreach will not cause problem. the behavior of the script is that it might not be what you were expecting because basically only the last result of the foreach is going to be sent to the browser. the easiest way to try to figure out what’s going on is to use a tool that diff between the file coming from the browser and the original, and see if there’s any difference at the beginning or end of the file, for example.

  • I get it. Being a test, I added only one entry in the BD and what happens is that this file stored in binary, when I download (already in pdf) and try to open, returns me the message that Acrobat Reader cannot open and that may not have been decoded correctly.

Browser other questions tagged

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