将Blob图片(存储在数据库中)下载到我的计算机上 [英] download blob image (stored in the database) to my computer
问题描述
我的Blob图片存储在数据库中,我想将其下载到设备中.
i have blob images that are stored in the database I want to download it to my device.
-
该图像将被下载(作为***.jpg),但已损坏. 这是我的download.php代码
the image will be download (as ***.jpg) but it is corrupted . this is my download.php code
<?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);
?>
感谢< 3
推荐答案
您输出到页面的所有内容均视为文件内容.我想您不希望"<br>"
出现在文件的内容中.
Everything you output to the page is considered a file contents. I suppose you don't want "<br>"
to be in the contents of your file.
第二点-在设置标题之前不要进行任何输出.
Second point - do not do any output before setting headers.
第三点-exit('string')
输出'string'
,因此您可以使用echo
和exit
输出文件两次:
Third point - exit('string')
outputs 'string'
, so you output content of your file twice: with echo
and with exit
.
因此,您的代码应如下所示:
So, your code should look like:
$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();
这篇关于将Blob图片(存储在数据库中)下载到我的计算机上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!