简体   繁体   中英

file download not working php

I need your suggestions on this issue.. my intention is, i have a word doc uploaded in db as BLOB content... and i have it displayed as a link in my page..

when i click on the link, the word doc should be downloaded... i get the below error msg, upon clicking the link.. Warning: Cannot modify header information - headers already sent by (output started at /home/stthohuu/public_html/sp/archive_newsletter.php:8) in

see code below...

</head>
<body>

        <?php
            //database connection
            $con = mysql_connect('localhost', 'abc', 'abc') or die(mysql_error());
            //select database
            $db = mysql_select_db('stthohuu_church', $con);
            $query = "SELECT id, name FROM newsletter order by id desc";
            $result = mysql_query($query) or die('Error, query failed');
            if(mysql_num_rows($result) == 0)
            {
            echo "No files found in DB<br>";
            } 
            else
            {
            while(list($id, $name) = mysql_fetch_array($result))
            {
            ?>
            <a href="archive_newsletter.php?id=<?php echo urlencode($id);?>"
            ><?php echo urlencode($name);?></a> <br>
            <?php 
            }
            }
            mysql_close();
        ?>
</body>
</html>
<?php
if(isset($_GET['id'])) 
{
// if id is set then get the file with the id from database
$con = mysql_connect('localhost', 'abc', 'abc') or die(mysql_error());
$db = mysql_select_db('stthohuu_church', $con);
$id    = $_GET['id'];
$query = "SELECT name, type, size, content " .
         "FROM newsletter WHERE id = '$id'";
$result = mysql_query($query) or die('Error, query failed');
list($name, $type, $size, $content) = mysql_fetch_array($result);
header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
ob_clean();
flush();
echo $content;
mysql_close();
exit;
}
?>

this also works in Xampp but NOT in web server :(

header() must be before any output. Also about - you need to start the output buffering(ob_start()) before you call ob_clean().

<?php
if(isset($_GET['id'])) 
{
ob_start();
// if id is set then get the file with the id from database
$con = mysql_connect('localhost', 'abc', 'abc') or die(mysql_error());
$db = @mysql_select_db('stthohuu_church', $con);
$id    = $_GET['id'];
$query = "SELECT name, type, size, content " .
         "FROM newsletter WHERE id = '$id'";
$result = mysql_query($query) or die('Error, query failed');
list($name, $type, $size, $content) = mysql_fetch_array($result);
header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
ob_clean();
echo $content;
mysql_close();
exit;
}
?>
</head>
<body>

        <?php
            //database connection
            $con = mysql_connect('localhost', 'abc', 'abc') or die(mysql_error());
            //select database
            $db = mysql_select_db('stthohuu_church', $con);
            $query = "SELECT id, name FROM newsletter order by id desc";
            $result = mysql_query($query) or die('Error, query failed');
            if(mysql_num_rows($result) == 0)
            {
            echo "No files found in DB<br>";
            } 
            else
            {
            while(list($id, $name) = mysql_fetch_array($result))
            {
            ?>
            <a href="archive_newsletter.php?id=<?php echo urlencode($id);?>"
            ><?php echo urlencode($name);?></a> <br>
            <?php 
            }
            }
            mysql_close();
        ?>
</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM