简体   繁体   中英

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

this is mysql table db table containg blob image

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.

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. but when i insert the image to the database with php the image doesn't show up.

<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

Try removing the 'addslashes' method call, you don't need it when using prepared statements the DB engine handles that. 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'.

So either:

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

Or

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

I recommend the first option.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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