1

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=

6
  • I forgot to say that $tasks = mysqli_query($db, "SELECT * FROM tasks"); //db is the connection Commented Aug 11, 2019 at 2:36
  • Have you moved that file in any folder? Because of you are storing image name to db. and in display view you are trying to access base 64? Commented Aug 11, 2019 at 3:09
  • I store it directly in the database as blob Commented Aug 11, 2019 at 3:18
  • Oh i understand, I am storing the name, not the file right? Commented Aug 11, 2019 at 3:18
  • No, you've to store that image in particular folder. and store that name in db. so that while displaying image you can fetch that image from folder I'm putting the code in answer please check. Commented Aug 11, 2019 at 3:20

1 Answer 1

2

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;  ?>' >
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.