简体   繁体   English

将Blob图片(存储在数据库中)下载到我的计算机上

[英]download blob image (stored in the database) to my computer

i have blob images that are stored in the database I want to download it to my device. 我有存储在数据库中的Blob图像,我想将其下载到我的设备。

  • the image will be download (as ***.jpg) but it is corrupted . 图像将被下载(作为***。jpg),但已损坏。 this is my download.php code 这是我的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 <3 thanx <3

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. 我想您不希望文件内容中包含"<br>"

Second point - do not do any output before setting headers. 第二点-在设置标题之前不要进行任何输出。

Third point - exit('string') outputs 'string' , so you output content of your file twice : with echo and with exit . 第三点exit('string')输出'string' ,因此两次输出文件内容: echoexit

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();

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

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