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

不羁岁月 提交于 2019-12-12 04:02:42

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!