douaonong7807 2014-02-19 11:54
浏览 35

PHP / MySQL - 从blob显示图像[重复]

This question already has an answer here:

I've made a php-page where you can upload images/files that will be saved in my database as the type "mediumblob". Now I'm stuck and can't figure out how to display these images from the database. I wrote in caps where I would like to print out the images.

<?php
$dbLink = new mysqli('127.0.0.1', 'root', '', 'test');
if(mysqli_connect_errno()) {
die("MySQL connection failed: ". mysqli_connect_error());
}

$sql = 'SELECT `id`, `name`, `mime`, `size`, `created`, `data` FROM `file`';
$result = $dbLink->query($sql);

if($result) {
// Kolla om det finns några filer i databasen
if($result->num_rows == 0) {
echo '<p>There are no files in the database</p>';
}
else {

echo '<table width="100%">
<tr>
<td><b>Name</b></td>
<td><b>Mime/type</b></td>
<td><b>Size (bytes)</b></td>
<td><b>Created</b></td>
<td><b>Image</b></td>
</tr>';

while($row = $result->fetch_assoc()) {
echo "
<tr>
<td>{$row['name']}</td>
<td>{$row['mime']}</td>
<td>{$row['size']}</td>
<td>{$row['created']}</td>
//THIS IS WHERE I'M SUPPOSED TO PRINT OUT THE IMAGES
</tr>";
}



echo '</table>';
}
}

else
{
echo 'Error! SQL query failed:';
echo "<pre>{$dbLink->error}</pre>";
}
echo '<p>Click <a href="index.html">here</a> to go back</p>';
// Close the mysql connection
$dbLink->close();
?>

This is my table in the database:

CREATE TABLE `file` (
`id` Int Unsigned Not Null Auto_Increment,
`name` VarChar(255) Not Null Default 'Untitled.txt',
`mime` VarChar(50) Not Null Default 'text/plain',
`size` BigInt Unsigned Not Null Default 0,
`data` MediumBlob Not Null,
`created` DateTime Not Null,
PRIMARY KEY (`id`)
) 

Very thankful for any help!

</div>
  • 写回答

3条回答 默认 最新

  • dsgfdgh14569 2014-02-19 11:59
    关注

    Try this;

    header('Pragma: public');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Content-Transfer-Encoding: binary');
    header('Content-length: '.$row['size']);
    header('Content-Type: '.$row['mime']);
    header('Content-Disposition: attachment; filename='.$row['name']);
    echo '<img src="'.$row['data'].'" />';
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端