简体   繁体   English

使用PHP从MySQL显示图像不起作用

[英]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: 和PHP代码:

$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 一切上传都可以,但是我有一些代码可以使用Bootstrap 4模式显示图像

<?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= 当我查看图像网址时,哪行图像显示一个小的图像正方形(错误),就像这样: 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;  ?>' >

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

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