我有blob图像存储在数据库中,我想将其下载到我的设备。
图像将被下载(如***。jpg),但它已损坏。 这是我的download.php代码
<?php
$servername = "localhost";
$dbusername = "root";
$dbpassword = "";
$dbname = "text_blob1";
$con= new mysqli($servername, $dbusername, $dbpassword, $dbname);
$id=$_GET["id"];
$sql = "select * from table1 where id=$id "; // 1
$res = $con->query($sql);
while($row = $res->fetch_assoc())
{
$name = $row['name'];
echo "<br>";
$size = $row['size'];
echo "<br>";
$type = $row['extension'];
echo "<br>";
$image = $row['image'];
}
header("Content-type: ".$type);
header('Content-Disposition: attachment; filename="'.$name.'"');
header("Content-Transfer-Encoding: binary");
header('Expires: 0');
header('Pragma: no-cache');
header("Content-Length: ".$size);
echo $image;
exit($image);
?>
thanx&lt; 3
答案 0 :(得分:2)
您输出到页面的所有内容都被视为文件内容。我想你不希望"<br>"
出现在你文件的内容中。
第二点 - 在设置标题之前不要做任何输出。
第三点 - exit('string')
输出'string'
,因此您输出文件的内容两次:echo
和exit
。
因此,您的代码应如下所示:
$id=$_GET["id"];
$sql = "select * from table1 where id=$id "; // 1
$res = $con->query($sql);
while($row = $res->fetch_assoc())
{
$name = $row['name'];
$size = $row['size'];
$type = $row['extension'];
$image = $row['image'];
}
header("Content-type: ".$type);
header('Content-Disposition: attachment; filename="'.$name.'"');
header("Content-Transfer-Encoding: binary");
header('Expires: 0');
header('Pragma: no-cache');
header("Content-Length: ".$size);
echo $image;
exit();