Error while downloading file getting binary data instead of the actually file

getting error while downloading file, getting binary data to the browser instead of the actually file, specially pdf files. im using BLOB data type in mysql filed.
also tried ob_end_clean but not helping.

v?—x¢b(‏°®ه– ç‏zٍڈà÷µï½zTَکêùQïىxùSَ–‏jîآ·×Yï¹oف5KK÷¾oصتمح+¾¦œ:و†ƒloوnأ"گ ±Oّ•زـ¦§w8^uLٹé†-ئûpfچâ+أجشƒœر د؛-ںe•ؤ…¬RH¦ hْDyے#½=@‘X’§¾ذXœtz”†رص0âLوٍ›“ء²{÷ڈے ½}f؛ِâB¦کژ¯“كگyeطPeüpy,¥¼تِŒً•MR±wگCي¢âٹ‘¬}Fzى3¢e¾NAو^4è¬{j=¤ر³جسâل<¶–5Mz&@+MYN1ُ¨Cںq)زêذش£M nj]T‡à×h0ï3D¤6غإQ.µg¬€ء¥w«ي|م+s“çكûmٍاe¯Œغ½وؤ~لà…½ں$/‡ھ(!·@‡]µظtحîز4»CQeں[°ظ5گ+è’ہù4›MQd™@ں|‡فژd؟×ë×Fxز‘ ¹’پ÷i·?ù×ـسخ½Mأ}ق¸َOe$ِ à’ظgءl”ƒ=fRن(rإهـPy¨2ش2ج:طQî49œt رA3kfفذ’){¯’7ذE=»نx¾ةLb|‘شاـ×s ل‡ةأجô‰—ثSùy¦y†uھc>ا/’›Zç9Vٍ·بش&¸ةq“s؟Yعlzگïگ_v¼ئ؟!؟د h=لّ’ےJ‏تْ…£ںبb÷U;Lآڑ›eڑƒ¨}كFپ ں›Uنvi>“]¤vçW†•Bڑˆˆ´!ج=¥4†é1حز ہحٹ‚é?“ل¢qع¬ ض4‹فلtڑgؤbوT§ةŒEچ8“س©#ôںآ‹EW9—ھr ‘8ژ§¦z$»±¸SW •¨xضث؛i›©سؤ™:pا¾YهسaکؤvC«صژkœچ “ژr\îW"Tùؤ’œ¥<غàû<§«،«ئ¶ —ًيFل¥رکً³ظ(WVتG{i®> stream xœه]ëڈeإqXXVثs1ث‚¹ ;†¹ôûAâDٹEت7[Hùùd’N"ضر’ےJUWَُô¹sg¦w±#ygخمVWuWWW?~Oâ(صAàü‏ةف¯çüك»éد‡ےxW‏ë®4ٌ£;Hkز؟ےؤƒSر~ü÷»ےQpئ¨C0؛{,=ƒ=َù‰ég”qً?™ïH،l{[:گJ›‏Uي¢؛ٍص,أوm)¤>ٌ6فے°R:^ùêî[ثٹ3¢¾m…ƒضŒ‡ إVeززF{{0BMˆڑں°îùخVيئ[ّ هYPüG6Eàغn{[£´Rآدù•د¥·ءىفس¾¸÷*‹~÷=ِ/

here is my php code for downloading

<?php


$company =$_GET['company'];

// Make sure an ID was passed
if(isset($_GET['id'])) {
// Get the ID
$id = intval($_GET['id']);
// Make sure the ID is in fact a valid ID
if($id <= 0) {
die('The ID is invalid!');
}
else {
// Connect to the database
$dbLink = new mysqli('localhost', 'sqldata', 'sqldata', 'balhaf');
if(mysqli_connect_errno()) {
    die("MySQL connection failed: ". mysqli_connect_error());
}

// Fetch the file information
$query = "
    SELECT mime, name, size, data
    FROM $company
    WHERE id = $id";
$result = $dbLink->query($query);

if($result) {
    // Make sure the result is valid
    if($result->num_rows == 1) {
    // Get the row
        $row = mysqli_fetch_assoc($result);

        // Print headers
        header("Content-Type: ". $row['mime']);
        header("Content-Length: ". $row['size']);
        header("Content-Disposition: attachment; filename=". $row['name']);

        // Print data
        echo $row['data'];
    }
    else {
        echo 'Error! No image exists with that ID.';
    }

    // Free the mysqli resources
    @mysqli_free_result($result);
    }
    else {
    echo "Error! Query failed: <pre>{$dbLink->error}</pre>";
    }
    @mysqli_close($dbLink);
    }
    }
    else {
    echo 'Error! No ID was passed.';
    }
    ?>

Try using the following as your content type which should trigger the download correctly.

Content-Type: application/octet-stream

Also I would highly recommend you steer clear of BLOB’s for storing data in especially for files as they are slow, consume a lot of memory and make debugging very hard compared to using real files stored on the disk which only consume I/O and CPU resources.

chris.upjohn, i tried using it, but doesn’t work.