0

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?

1
  • why are you using fetch_array() AND result()? you've already got a row fetched, why not just $nome = $row['xml_chNfe']? Commented Aug 17, 2015 at 14:58

1 Answer 1

1
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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank's!! Solve my problem!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.