简体   繁体   中英

Display image from MySQL using PHP not working

I have an upload image to database code like this:

<form method="post" action="quanly.php" class="form-group justify-content-center" enctype="multipart/form-data">
               <div class="custom-file mb-3">
                 <input type="file" class="custom-file-input" id="image" name="image">
                 <label class="custom-file-label" for="customFile">Choose file</label>
               </div>
               <div class="col text-center">
                  <button type="submit" name="submit" id="add_btn" class="btn btn-primary mb-2"> <i class="fas fa-plus"></i>Submit</button>
               </div>
            </form>

And the PHP code:

$image = addslashes($_FILES['image']['name']);
   $query = "INSERT INTO tasks (image) VALUES ('$image')";
   mysqli_query($db, $query); //db is the mysql connection

Everything upload works just fine, but I have some code to display image using Bootstrap 4 modal

<?php $i = 1; while ($row = mysqli_fetch_array($tasks)) { ?>
<button type="button" class="btn btn-primary btn-sm " data-toggle="modal" data-target="#imagemodal<?php echo $row['id'];?>" <?php if (empty($row['image'])) { echo "disabled"; } ?> ><i class="fas fa-image"></i></button> 
<!-- IMAGE MODAL BEGIN -->    
      <div class="modal fade" id="imagemodal<?php echo $row['id'];?>">
  <div class="modal-dialog">
    <div class="modal-content">

      <!-- Modal Header -->
      <div class="modal-header">
        <h4 class="modal-title">Xem ảnh/ đính kèm</h4>
        <button type="button" class="close" data-dismiss="modal">&times;</button>
      </div>

      <!-- Modal body -->
      <div class="modal-body">
        <?php echo '<img src="data:image/jpeg;base64,'.base64_encode( $row['image'] ).'"/>'; ?>
      </div>

      <!-- Modal footer -->
      <div class="modal-footer">
        <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
      </div>

    </div>
  </div>
</div>
<!-- IMAGE MODAL END -->  
<?php $i++; } ?>    

Which image of rows showing a small image square (error), when I view the image url, it's something like this: data:image/jpeg;base64,ei5wbmc=

Store and upload image in folder code

<?php


    if(isset($_POST['but_upload'])){

      $name = $_FILES['file']['name'];
      $target_dir = "upload/";
      $target_file = $target_dir . basename($_FILES["file"]["name"]);

      // Select file type
      $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

      // Valid file extensions
      $extensions_arr = array("jpg","jpeg","png","gif");

      // Check extension
      if( in_array($imageFileType,$extensions_arr) ){

         // Insert record
         $query = "insert into images(name) values('".$name."')";
         mysqli_query($con,$query);

         // Upload file
         move_uploaded_file($_FILES['file']['tmp_name'],$target_dir.$name);

      }

    }
    ?>

    <form method="post" action="" enctype='multipart/form-data'>
      <input type='file' name='file' />
      <input type='submit' value='Save name' name='but_upload'>
    </form>

Show in display view.

<?php

$sql = "select name from images where id=1";
$result = mysqli_query($con,$sql);
$row = mysqli_fetch_array($result);

$image = $row['name'];
$image_src = "upload/".$image;

?>
<img src='<?php echo $image_src;  ?>' >

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