[英]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.