Extension . pdf does not download php

Asked

Viewed 76 times

2

Good night,

I have a table in the database that registers files(pdf,img,png and etc...) called

billets:

CREATE TABLE boletos ( 
 protocolo int(11) NOT NULL AUTO_INCREMENT,
 boleto mediumblob NOT NULL,
 nomeBoleto varchar(20) DEFAULT NULL,
 tipoBoleto varchar(20) DEFAULT NULL,
 PRIMARY KEY (protocolo));

I was able to register in bytes[] of the files and pull from the bank and download, all the files download perfectly, only the pdf that does not download with its extension (.pdf), it downloads with the extension of (all the files) and I have to click on top to choose for it to open in pdf, someone knows why it happens only with pdf, my code is like this:

download php.:

<?php
include "config.php";
// Define o tempo máximo de execução em 0 para as conexões lentas
    set_time_limit(100);

$GerParam=filter_input(INPUT_GET, "protocolo" ,FILTER_DEFAULT);
if(isset($_GET['protocolo'])){
     $protocolo = $_GET['protocolo'];
     $stat = $db->prepare("SELECT nomeBoleto,tipoBoleto,boleto FROM boletos WHERE protocolo=?");

    $stat->execute(array($_GET['protocolo']));
    $stat->bindColumn(1, $nomeBoleto, PDO::PARAM_STR, 256);
    $stat->bindColumn(2, $tipoBoleto, PDO::PARAM_STR, 256);
    $stat->bindColumn(3, $boleto, PDO::PARAM_LOB);
    $stat->fetch(PDO::FETCH_BOUND);

                header('Accept-Ranges: bytes');
                header('Content-Transfer-Encoding: binary');
                header("Content-Type: $tipoBoleto"); 
                header("Content-Disposition: attachment; filename=$nomeBoleto");
                echo $boleto;


}

?>

All download with the right extension only the pdf that does not...

1 answer

3


Check if the name of the boleto is correct, a possibility is the name of the boleto be without the extension PDF, no filename=$nomeBoleto, if the boleto name is empty, you will have no problem, however if you have a name, if you do not have the boleto extension, problems may occur.

Suggested field size change nomeBoleto and typeBollet

CREATE TABLE boletos 
  ( 
     protocolo  INT(11) NOT NULL auto_increment, 
     boleto     MEDIUMBLOB NOT NULL, 
     nomeboleto VARCHAR(255) DEFAULT NULL, 
     tipoboleto VARCHAR(50) DEFAULT NULL, 
     PRIMARY KEY (protocolo) 
  );
<?php
include "config.php";
// Define o tempo máximo de execução em 0 para as conexões lentas
set_time_limit(100);
$protocolo = filter_input(INPUT_GET, "protocolo", FILTER_DEFAULT);
if (isset($protocolo)) {
    $sql = "SELECT nomeBoleto, tipoBoleto, boleto FROM boletos WHERE protocolo= :protocolo";
    $stat = $db->prepare($sql);
    $stat->bindParam(':protocolo', $protocolo, PDO::PARAM_INT);
    $stat->execute();
    $result = $stat->fetch(PDO::FETCH_ASSOC);

    if ($result) {
        $nomeBoleto = $result["nomeBoleto"];
        $tipoBoleto = $result["tipoBoleto"];
        $boleto = $result["boleto"];

        $extensoes_usadas = array('.jpg', '.gif', '.png','.pdf','.img');                
        if(!in_array($nomeBoleto, $extensoes_usadas) === true){
            $nomeBoleto = "";
        }

        header('Accept-Ranges: bytes');
        header('Content-Transfer-Encoding: binary');            
        header("Content-Type: $tipoBoleto");
        header("Content-Disposition: attachment; filename=$nomeBoleto");
        echo $boleto;
    }
}
  • 1

    It was perfect @Oliveira, just change the fieldName to Varchar(255) as you suggested, already solved the problem, mto thanks msm.. :)

Browser other questions tagged

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