【问题标题】:download blob image (stored in the database) to my computer将 blob 图像(存储在数据库中)下载到我的计算机
【发布时间】:2017-08-17 23:56:39
【问题描述】:

我有存储在数据库中的 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);   
       ?>
    

感谢

【问题讨论】:

    标签: php mysql image mysqli blob


    【解决方案1】:

    您输出到页面的所有内容都被视为文件内容。我想您不希望 "&lt;br&gt;" 出现在您的文件内容中。

    第二点 - 在设置标题之前不要做任何输出。

    第三点 - exit('string') 输出 'string',因此您输出文件的内容两次echoexit

    因此,您的代码应如下所示:

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

    【讨论】:

    • 好吧,我今天学到了一些东西。非常感谢它的工作
    猜你喜欢
    • 2021-05-22
    • 2014-03-17
    • 1970-01-01
    • 2017-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-26
    相关资源
    最近更新 更多