henrique romao
henrique romao

Reputation: 560

Download empty BLOB file from MySQL DB PHP

I'm trying to download a XML file from my DB. The file is stores in a BLOB field, but when I open the page it dowloads a blank xml file.
This is my function:

<?php 
    $conexao = mysql_connect("db","user","pss") or die('Not connected : ' . mysql_error());
    mysql_select_db("db", $conexao) or die (mysql_error());
    $result = mysql_query("select * from xmlNFe where xml_id = 1", $conexao);
    $row = mysql_fetch_array($result);
    $xml =  $row['xml_xml'];
    $nome = mysql_result($result, 0, 'xml_chNFe');

    header('Content-type: application/octet-stream');
    header('Content-Description: File Transfer');
    header("Pragma: public");
    header("Content-Disposition: attachment;filename=".$nome.".xml"); 
    header('Content-Transfer-Encoding: binary');
    header("Content-length: ".filesize($xml));
    header('Accept-Ranges: bytes');
    ob_clean();
    flush();
    echo $xml;
    mysql_close($conexao);
    exit;
?>

Does anyone have any idea?

Upvotes: 0

Views: 519

Answers (1)

Marc B
Marc B

Reputation: 360572

header("Content-length: ".filesize($xml));
                                   ^^^^

$xml is your actual xml data, it's NOT a filename, so filesize() will FAIL and return boolean FALSE. You cannot get the size of a file which does not exist.

Try

header("Content-length: ".strlen($xml));
                          ^^^^^^

instead.

Upvotes: 1

Related Questions