如何用数据库表的下一个Blob条目替换Blob中的单词

时间:2019-05-28 07:00:40

标签: php mysql

我正在尝试构建一个页面,该页面能够读取表格并将所选条目(时间,发件人,收件人,标题和内容)写下来,并且效果很好。

我面临的问题是第一个内容条目是这样的文本BLOB

ex.
TextTextTextTextTextText 
TextTextTextText
img1.jpg
TextTextTextTextText
TextTextTextTextTextText

img1.jpg保存在下一个BLOB内容条目中,必须经过base64编码才能作为图像打印,现在我被困在尝试将其写下来时

TextTextTextTextTextText
TextTextTextText
(actual image from the next entry)
TextTextTextTextText
TextTextTextTextTextText

我尝试用strpos检查内容是否包含下一个Blob的文件名,然后将其替换为实际图像,但是我无法设法在同一行中获取下一个表条目< / p>

这是正常输出的代码

$res = mysqli_query($link, createRequest(true, $link)) or die(mysqli_error($link));
while($row = mysqli_fetch_assoc($res)){

    $title = $row["a_subject"];
    $time = $row["create_time"];
    $sender = $row["a_from"];
    $reciever = $row["a_to"];
    $body = $row["content"];
    $articleid = $row["id"];

    if (strpos($body, '')){
        $img = "<pre>".'<img src="data:image/jpeg;base64,'.base64_encode( $body).'"/>'."</pre>"

    ?>
    <tr>
    <td><?php echo ($time) ?></td>
    <td><?php echo ($sender) ?></td>
    <td><?php echo ($reciever) ?></td>
    <td><?php echo ($title) ?></td>
    <td><?php echo $img ?></td>
    <td><a href=<?php echo "http://localhost/php/imageviewer.php?ArticleID=".$row["id"].""?>>Zeige Anhänge</a></td>
    </tr>
    <?php
    } else {
        ?>
    <tr>
    <td><?php echo ($time) ?></td>
    <td><?php echo ($sender) ?></td>
    <td><?php echo ($reciever) ?></td>
    <td><?php echo ($title) ?></td>
    <td><?php echo utf8_encode($body)?></td>
    <td><a href=<?php echo "http://localhost/php/imageviewer.php?ArticleID=".$row["id"].""?>>Zeige Anhänge</a></td>
    </tr>
    <?php

现在,我正在检查内容是图像还是只是一些文本,然后将其写下来。我想发生的事情是,可以说文本内容是:

text
img1
text
img2
text
img3 

我当前的解决方案有4个html表项 首先是

text
img1.jpg
text
img2.jpg
text
img3 .jpg

第二个是

actual image of img1.jpg

第三个存在

actual image of img2.jpg

我最后想要的是一个看起来像这样的表项

text
actual img1
text
actual img2
text
actual img3

1 个答案:

答案 0 :(得分:0)

好吧,据我所知,此表中有一些行是要显示的实际内容,包括对某些图像文件名的引用,然后在同一表中将这些实际图像存储为base64编码的BLOB。

我无法测试...,因此可以尝试一下,看看它是否提供您感兴趣的内容。

我评论了自己的添加内容,以使其更加清晰。

哦,我也使用了<?=的简写,纯粹是因为我认为它使代码更易于阅读/更易于使用。显然,如果您不想这样做,就不需要使用它!

<?php

<<PUT ANY OTHER EXISTING CODE HERE, E.G. THE START OF YOUR SCRIPT>>
$res = mysqli_query($link, createRequest(true, $link)) or die(mysqli_error($link));

// This function serves a dual purpose
// Given an image filename, e.g. `image001.png`, it will search the table for this filename and (hopefully) return a base64 encoded BLOB
// It then decodes this content and returns it
function get_image_from_filename ($filename, $db_connection) {
    $query = "SELECT article_attachment.content FROM article JOIN ticket ON article.ticket_id = ticket.id JOIN article_attachment on article.id = article_attachment.article_id WHERE article_attachment.filename = '$filename'";

    $res = mysqli_query($db_connection, $query) or die(mysqli_error($db_connection));

    while ($row = mysqli_fetch_assoc($res)) {
        return base64_decode($row["content"]);
    }
}

while($row = mysqli_fetch_assoc($res)) {

    $title = $row["a_subject"];
    $time = $row["create_time"];
    $sender = $row["a_from"];
    $reciever = $row["a_to"];
    $body = $row["content"];
    $articleid = $row["id"];

    if (base64_encode(base64_decode($body, true)) === $data){
        // If the `$body` can be decoded/encoded successfully, this row is an image - let's skip it
        continue;
    }
    else {
        // Otherwise, let's find all of the references to our images, and look them up in the database

        // This pattern will find the CID references and retrieve the associated image's filename
        $pattern = '/(?:cid:)([a-zA-Z0-9]+\.[a-zA-Z0-9]+)(@[a-zA-Z0-9]+\.[a-zA-Z0-9]+)+/';
        // This is the replacement that will be put into the `$body` variable, using the function at the top of this script
        $replacement = "<pre>" . '<img src="data:image/jpeg;base64,' . get_image_from_filename('$1', $link) . '" />' . "</pre>";

        $body = preg_replace($pattern, $replacement, $body);

?>

<tr>
    <td><?=$time?></td>
    <td><?=$sender?></td>
    <td><?=$receiver?></td>
    <td><?=$title?></td>
    <td><?=$body?></td>
    <td><a href="http://localhost/php/imageviewer.php?ArticleID=<?=$articleid?>">Zeige Anhänge</a></td>
</tr>

<?php

    } // End base64 if

}