繁体   English   中英

文件下载不起作用php

[英]file download not working php

我需要你对这个问题的建议。

当我点击链接时,应该下载 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 但不适用于 Web 服务器 :(

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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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