0

while downloading file im getting this error. specially pdf files.

and also program like download manger doesn't work on the downloading.

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؟×ë×F*xز‘ ¹’پ÷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ر~ü÷»ےQ‍pئ¨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.';
        }
        ?>
hadi
  • 3
  • 4

2 Answers2

0

use BLOB in MySQL to prevent that the data is changed due to the encoding (there might be encoding issues which produce your errors, saving as BLOB is binary safe)

related to Binary Data in MySQL

it may be better to store a link as file reference in the database instead of saving the actual content of the file and retrieve the files using file_get_contents and other related functions to get the mimetype (or finfo) ...

take a look at Trying to download Blob via PHP / MySQL and http://www.sitepoint.com/forums/showthread.php?693871-Problem-using-PHP-to-pull-binary-files-from-a-BLOB-field-in-MYSQL

Community
  • 1
  • 1
Daniel Ruf
  • 6,949
  • 10
  • 55
  • 108
  • i want to store the files in database. im using mediumblob and getting this binary error in my webpage when i click download. – hadi Aug 29 '13 at 21:35
  • take a look at http://stackoverflow.com/questions/15009672/trying-to-download-blob-via-php-mysql and http://www.sitepoint.com/forums/showthread.php?693871-Problem-using-PHP-to-pull-binary-files-from-a-BLOB-field-in-MYSQL – Daniel Ruf Aug 30 '13 at 10:35
  • tried all the example in web sites you giving me but not working same problem – hadi Aug 30 '13 at 22:16
0

Try flushing the output buffer immediately before you start setting headers with ob_end_clean, and then exiting immediately after the echo. Take a look at the section of the PHP book on Output Control.

The other possibility is that you are running out of memory when attempting to echo the data, and the error is getting obfuscated by all the preceding binary. Try splitting the binary in to smaller chunks (substr is binary safe), iterating over them and returning them to the output buffer.

BenLanc
  • 2,169
  • 16
  • 22
  • i tried to do ob_end_clean be for the header and exit after echo, but doesn't work same problem. how to use substr in my code can you please explain to me. – hadi Aug 30 '13 at 18:20