使用 PHP 显示来自 MySQL 的图像不起作用

我有一个上传图像到数据库代码是这样的:


<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>

和 PHP 代码:


$image = addslashes($_FILES['image']['name']);

   $query = "INSERT INTO tasks (image) VALUES ('$image')";

   mysqli_query($db, $query); //db is the mysql connection

一切上传工作正常,但我有一些代码可以使用 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++; } ?>    

哪行图像显示一个小图像方块(错误),当我查看图像 url 时,它是这样的: data:image/jpeg;base64,ei5wbmc=


梵蒂冈之花
浏览 173回答 1
1回答

波斯汪

在文件夹代码中存储和上传图像<?php&nbsp; &nbsp; if(isset($_POST['but_upload'])){&nbsp; &nbsp; &nbsp; $name = $_FILES['file']['name'];&nbsp; &nbsp; &nbsp; $target_dir = "upload/";&nbsp; &nbsp; &nbsp; $target_file = $target_dir . basename($_FILES["file"]["name"]);&nbsp; &nbsp; &nbsp; // Select file type&nbsp; &nbsp; &nbsp; $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));&nbsp; &nbsp; &nbsp; // Valid file extensions&nbsp; &nbsp; &nbsp; $extensions_arr = array("jpg","jpeg","png","gif");&nbsp; &nbsp; &nbsp; // Check extension&nbsp; &nbsp; &nbsp; if( in_array($imageFileType,$extensions_arr) ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Insert record&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$query = "insert into images(name) values('".$name."')";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mysqli_query($con,$query);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Upload file&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;move_uploaded_file($_FILES['file']['tmp_name'],$target_dir.$name);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; ?>&nbsp; &nbsp; <form method="post" action="" enctype='multipart/form-data'>&nbsp; &nbsp; &nbsp; <input type='file' name='file' />&nbsp; &nbsp; &nbsp; <input type='submit' value='Save name' name='but_upload'>&nbsp; &nbsp; </form>在显示视图中显示。<?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;&nbsp; ?>' >
打开App,查看更多内容
随时随地看视频慕课网APP