文件下载不工作的PHP

时间:2014-04-20 10:22:24

标签: php file

我需要你就这个问题提出建议.. 我的意图是,我在db中上传了一个单词doc作为BLOB内容......我将它显示为我页面中的链接..

当我点击链接时,应该下载单词doc ... 单击链接后,我收到以下错误信息。     警告:无法修改标题信息 - 已经发送的标题(输出从/home/stthohuu/public_html/sp/archive_newsletter.php:8开始)

见下面的代码......                   

</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;
}
?>

这也适用于Xampp,但不适用于网络服务器:(

1 个答案:

答案 0 :(得分:1)

header()必须在任何输出之前。 另外 - 你需要在调用ob_clean()之前启动输出缓冲(ob_start())。

<?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>