问题
i have blob images that are stored in the database I want to download it to my device.
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); ?>
thanx <3
回答1:
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.
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();
来源:https://stackoverflow.com/questions/43009763/download-blob-image-stored-in-the-database-to-my-computer