简体   繁体   English

如何使用 PHP 从 MYSQL 插入和检索图像 blob?

[英]How to insert and retrieve image blob from MYSQL using PHP?

this is mysql table db table containg blob image这是包含 blob 图像的mysql 表db 表

what i'm trying to do is insert an image and store in ' justification' as a blob and then retrieve it and display it in a table in another page.我想要做的是插入一个图像并作为 blob 存储在“理由”中,然后检索它并将其显示在另一个页面的表格中。

this is my code for displaying it :这是我的显示代码:

<?php 

  while ($row = $rep->fetch()) {  //$row contains the previous table's data
    $image = $row['justification'];   //justification is the blob image
    $encoded_image = base64_encode($image);   ?>
    <tr>
      <td><?php echo $row['ncarte']; ?></td>
      <td><?php echo $row['module']; ?></td>
      <td><?php echo $row['type']; ?></td>
      <td><?php echo $row['dateabs']; ?></td>
      <td><?php echo "<a href='data:image/png;base64,{$encoded_image}' download> <img height='30px' src='data:image/png;base64,{$encoded_image}'> </a>";?> </td>
    </tr>
    <?php } ?>

this code works perfectly if i insert the image manually from phpMyAdmin.如果我从 phpMyAdmin 手动插入图像,则此代码完美运行。 but when i insert the image to the database with php the image doesn't show up.但是当我用 php 将图像插入数据库时​​,图像没有显示出来。

<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>"  enctype="multipart/form-data" >
        <label for="module" >Module :</label>
        <select name="module">
            <?php   foreach ($module as $mod) {  ?>
            <option value="<?php echo $mod; ?>" > <?php echo $mod; ?> </option>
            <?php }  ?>
        </select>
        <br>
        <label for="type" >type :</label>
        <select name="type">
            <?php   foreach ($type as $ty) {  ?>
            <option value="<?php echo $ty; ?>" > <?php echo $ty; ?> </option>
            <?php }  ?>
        </select>
        <br>
        <label for="date" >Date :</label>
        <select name="date">
            <?php   foreach ($date as $dat) {  ?>
            <option value="<?php echo $dat; ?>" > <?php echo $dat; ?> </option>
            <?php }  ?>
        </select>
        <br>
        <input type="file" name="image"/>
        <br>
        <input type="submit" name="submit"/>
    </form>

    <?php  
    if(count($_FILES) > 0) {
        if(is_uploaded_file($_FILES['image']['tmp_name'])) {
            $date_of_abs = $_POST['date'];
            $type_of_abs = $_POST['type'];
            $module_of_abs = $_POST['module'];
            $imgData =addslashes(file_get_contents($_FILES['image']['tmp_name']));
            $sql = $bdd->prepare("UPDATE abs SET justification=?  WHERE ncarte=? and module=? and type=? and dateabs =?"); 
            $sql->execute(array(,$imgData,$_SESSION['etudiant'],$module_of_abs,$type_of_abs,$date_of_abs));

        }}
        ?>

this is output : in the first row i inserted the image from phpMyAdmin manually in the second row i inserted the same image with code html table displaying the 2 images这是输出:在第一行中,我在第二行中手动插入了来自 phpMyAdmin 的图像,我插入了相同的图像,代码html 表显示了 2 个图像

Try removing the 'addslashes' method call, you don't need it when using prepared statements the DB engine handles that.尝试删除 'addslashes' 方法调用,在使用数据库引擎处理的准备好的语句时不需要它。 This is likely your problem as adding extra characters would break the regular image.这可能是您的问题,因为添加额外字符会破坏常规图像。

If you insist on keeping 'addslashes' try wrapping the image data from the database in a 'stripslashes' call to reverse the effects from 'addslashes'.如果您坚持保留 'addslashes',请尝试将数据库中的图像数据包装在 'stripslashes' 调用中,以反转 'addslashes' 的效果。

So either:所以要么:

$imgData = file_get_contents($_FILES['image']['tmp_name']);

Or或者

$encoded_image = base64_encode(stripslashes($image));

I recommend the first option.我推荐第一个选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM